How does the Android life cycle demo make a dialogue box?

| | August 5, 2015

I am looking at the life cycle demo provided on the android dev website (http://developer.android.com/training/basics/activity-lifecycle/index.html). It makes a dialogue box appear when the pause button is clicked, but I cannot figure out where in the code it makes the dialogue activity into a dialogue box instead of a normal activity. I am trying to implement this in my own app so that I can experiment with pausing, but I just do not understand where the dialogue box comes from. Where is the code for making the activity appear as a dialogue box?

Here is the code for the UI

<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2012 The Android Open Source Project

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="225dp"
    android:layout_height="120dp"
    android:background="@color/dark_yellow"
    android:padding="12dip"
    >

<TextView  
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/dialog_text"
    android:gravity="center_horizontal"
    android:textSize="@dimen/font_medium"
    android:textColor="@color/light_yellow"
    android:paddingBottom="12dip"
    />

 <Button
     android:id="@+id/btn_finish_dialog"
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/btn_finish_dialog_label"
     android:layout_gravity="center_horizontal"
     android:onClick="finishDialog"
     />
</LinearLayout>

Here is the code for the class associated with the UI

/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an “AS IS” BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.android.lifecycle;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;

public class DialogActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_dialog);
    }

    /**
     * Callback method defined by the View
     * @param v
     */
    public void finishDialog(View v) {
        DialogActivity.this.finish();
    }
}

2 Responses to “How does the Android life cycle demo make a dialogue box?”

  1. The dialog appears when the button is clicked. The handler method is:

    public void startDialog(View v) {
        Intent intent = new Intent(ActivityA.this, DialogActivity.class);
        startActivity(intent);
    }
    

    This handler is defined in the Activity*.java files.

    This handler is mapped to the button via the onClick property in the activity_*.xml files:

       <Button
            android:id="@+id/btn_start_dialog"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="@string/btn_start_dialog_label"
            android:layout_toRightOf="@id/btn_finish_a"
            android:onClick="startDialog"
            />
    

    This property specifies what function on the activity should be called in the event the user clicks the button.

    When the DialogActivity is started, it loads its layout from the activity_dialog.xml file.

    As @RSenApps points out, to make the activity look like a dialog you need to specify the theme in AndroidManifest.xml

        <activity android:name=".DialogActivity"
                  android:theme="@android:style/Theme.Dialog">
        </activity>
    
  2. Not sure about this specific example, but in general, to make an activity look like a dialog, place the theme in your manifest (under your activity):

    <activity android:theme="@android:style/Theme.Dialog" />
    

Leave a Reply