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" />
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 ?