In my application i am trying to open a dialog box when activity started and it has a positive button. on click on that button a new activity will open. Its working fine with following code. But when I long press menu button or search button dialog box get disappear. how can I make this dialog box remain constant even if i press menu or search button. thanks.
public class Sam3Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView title = new TextView(this);
title.setText("DM2");
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
/* alert message */
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCustomTitle(title);
builder.setMessage("dialog with message").setCancelable(false).setPositiveButton(
"Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
Log.d("choose","on dissmiss");
startActivity(new Intent(getApplicationContext(),
StatisticDisplay.class));//second activity
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Take care of the Keys pressed by attaching an
onKeyListenerto theDialog, in my example I’m just taking care of the search key, but you should get the point. In case you’ve never worked with listeners like this, returningtruemeans the event should not propagate and is therefor taken care of preventing the system from catching thesearchkey when pressed while showing the dialog.In any other case (the key not being the
KEYCODE_SEARCH), it will just pass this listener as if it didn’t exist. 🙂Relevant documentation: