gsmaker
gsmaker

Reputation: 211

AlertDialog disappears after switching activity

I have an android application having an AlertDialog with OK and Cancel buttons. When the dialog shows without pressing the OK or Cancel button, just press Home button of device. The Home screen will show. Now open another application suppose Camera. Take some picture or Video. Now get out from the Camera application. Now open my android application and surprisingly the alertdialog have disappeared. Why?

Upvotes: 1

Views: 2467

Answers (2)

Mitul Nakum
Mitul Nakum

Reputation: 5574

If you want to show dialog on startup of application then write this code in

onResume()

method, it will show dialog every time when user returns to this screen. Or you can manage its state in

onPause() 

Upvotes: 1

alexanderjslin
alexanderjslin

Reputation: 1149

I'm guessing you are creating this AlertDialog onCreate() method. First, you should read up on the Activity Lifecycle.

And what happens is that when you go to another app, the Activity goes to onPause method, which cleans up a bit.

Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.

Then because you return to the app, it calls the onResume method, which doesn't create your dialog again.

Upvotes: 2

Related Questions