coderplus
coderplus

Reputation: 5913

Android Dialog doesn't disappear completely when User tries to go back

I created a dialog activity using the following snippet. I'm using a translucent theme for this activity.So it looks neat.

public class DialogActivity extends Activity {
AlertDialog alertDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Title");
    alertDialog.setMessage("Body");
    alertDialog.setIcon(R.drawable.ic_launcher);
    aleratDialog.setButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            DialogActivity.this.finish();
        }
    });

    alertDialog.show();
}

@Override
protected void onPause() {
    if(alertDialog!=null) {alertDialog.dismiss();}
    super.onPause();

}

@Override
protected void onStop() {
    if(alertDialog!=null) {alertDialog.dismiss();}
    super.onStop();

}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}


}

I am having trouble when the user clicks on the Back button. The activity still stays on foreground when the user does that. I tried overriding the onKeyDown method to call finish() when the user tries to go back but this didn't help.

Note: The onBackPressed or onKeyDown methods are not invoked when I press back for the first time.(The ActionBar stays) I have to press it a second time to get there and then the activity actually disappears

I think this has got something to do with my Manifest entry

<activity
        android:name=".DialogActivity"
        android:launchMode="singleInstance"
        android:noHistory="true"
        android:label="@string/app_name"
        android:configChanges="orientation"
        android:theme="@android:style/Theme.Holo.Dialog" />

Upvotes: 0

Views: 904

Answers (5)

Alican Temel
Alican Temel

Reputation: 1318

You can use EventBus to listen DialogActivity events from another Activities. Or, you can implement inteface to listen.

EventBus(GreenRobot) examples:

https://github.com/greenrobot/EventBus

https://greenrobot.github.io/EventBus/

http://code.tutsplus.com/tutorials/quick-tip-how-to-use-the-eventbus-library--cms-22694

Upvotes: 1

Hassan TBT
Hassan TBT

Reputation: 873

change this

android:launchMode="singleInstance"

to

android:launchMode="singleTask" or android:launchMode="singleTop"

Upvotes: 0

yoah
yoah

Reputation: 7230

What you are doing is create a dialog (the DialogActivity), and from it open another dialog with the alert builder. So you get 2 dialogs, and clicking back removes the alert dialog, but not the DialogActivity. Why do you need the DialogActivity? Why not open the alert from the calling activity ?

Upvotes: 1

Anup Cowkur
Anup Cowkur

Reputation: 20563

Try using

public void onBackPressed() {
    if(alertDialog!=null) 
    alertDialog.dismiss();
    DialogActivity.this.finish();
    super.onBackPressed();
}

Upvotes: 0

G M Ramesh
G M Ramesh

Reputation: 3412

As you are calling your Dialog in onCreate(), the dialog will be on foreground.

So try to finish the dialog onBackPressed in onKeyDown Method.

Upvotes: 0

Related Questions