In my app I have the following structure:
Activity A calls activity B, and finishes, removing itself from the stack; Activity B receives an intent from activity A that specifies some action to perform, when these actions are performed, the user will see a message indicating it (still in activity B) .If the user presses the home button the app will go to background, calling "onPause" and then "onStop". All works as expected.
The problem is that if the user leaves the app in background for a few hours, and tries to open the app again (after a few hours) the onCreate method in activity B will be called with the exact same intent as it received some hours ago from activity A.
(if the user goes back to the app a few minutes after, he will see activity B just like when he left)
What I wanted was that in this case my app would open activity A and not activity B or, activity B but with the message that was on screen when user left.
Looking at the activity life cycle it seems that the app is killing by the OS, all of this makes sense, what I really don’t understand is why activity B opens with onCreate method and the same intent as before.
Use
onSaveInstanceState()to record the state of ActivityB. Android will call this before it kills the process. When the user returns to the application, it will recreate the process and then recreate whatever activity was on the top of the activity stack (in your case, ActivityB). When it does that, it will pass the saved instance state to the activity as a parameter in theonCreate()call. You should be able to put something in there so that your ActivityB can tell that it is being recreated after a process kill/restart. In that case, ActivityB could just show the user the message again (which it would have to save in theonSaveInstanceState()method) or it could just redirect the user back to ActivityA so that he can start all over again.