Call alertDialog onStop/onPause

2019-05-27 03:20发布

@Override
protected void onStop() {
    super.onStop();

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Test message")
           .setCancelable(true)
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
               }
           });
    AlertDialog alert = builder.create();
    alert.show();
}

This obviously isn't working, as soon as dialog is shown - activity is stopped (dialog disappears). How to solve this issuse?

I want to save some setting into my database as soon as activity is left (via back button, clicking on some button which leads to some other activity, click on notification and so on..) and then show the result in AlertDialog?

Or even better - when Android recognizes that activity will be closed, it saves my settings, show AlertDialog and then onClick Activity is finally closed.

3条回答
再贱就再见
2楼-- · 2019-05-27 03:28

You should call super.onStop() after you are done with your processing.

查看更多
Bombasti
3楼-- · 2019-05-27 03:36

I guess you need to send settings when the user exits the application or application envokes onPause(). Why don't you try to grasp all ways of exiting application lets say Back button pressed, so add listener for all these events. And do the sending.

查看更多
Viruses.
4楼-- · 2019-05-27 03:42

It's a very bad practice to try to do something that takes time and user attention in onStop and onPause. Usually these methods are used to save some data. You can try to show a Toast, but the best way - is to show nothing, as it's not an usual practice. Is there really something important you need to show? This is the question you must resolve in first place.

查看更多
登录 后发表回答