I have an activity that is themed as a dialog. I have seen that if the dialog is showing, and then I press the home button, and then using the task manager, restart the app, that dialog activity will be the activity that the app starts in, with no other activities available to go back to. That is, the activity that was running when I loaded the dialog activity is not running. So I just have this dialog-themed activity hovering over the desktop. That makes sense.
Looking over the Android activity lifecycle, the OS does remember the last activity and attempts to restart there. So I created all of the on* methods in my activity (onResume, onRestart, etc). What I found was really puzzling. When I restart the app from the task manager, the following methods are called:
onCreate()
onResume()
onStop()
onDestroy()
Where I was really just expecting
onRestart()
onCreate()
onResume()
- Why are onStop and onDestroy getting called right away? And why does the dialog still show, even though onDestroy is called?
- How can I configure this app so that it never starts solely on this dialog? I would be fine with the app restarting with the same “parent” activity and the dialog above it (that is, just as I left it), or with just the parent activity running and the dialog dismissed.
In this case, you should use a call to
finish()in your Dialog code. You want to do this when the user transitions away from your app (which can happen when they go to the home button, they get a call, etc…). In this case, you would want to make a call tofinish()in theonStop()of the Dialog. Calls to finish the current activity remove it from the stack, getting you essentially the behavior you describe.