1

I know that in Android, if stuff is idle for a while, the operating system will devour things to free up memory.

So if I have a first Activity, and I invoke a second Activity by using an Intent, and then invoke a third Activity using yet another Intent, I can use the back button to go back to the previous Activities.

But let's say I stay on the third Activity, and let the phone idle for a while until the OS decides to devour my app for memory. If I open the app again, will I have lost the stack I have formed from my Intents? Will I still be in the third Activity with the ability to press Back and go to Activity 2, then Activity 1?

user6419910
  • 155
  • 2
  • 12
  • OS takes care of calling Lifecycle methods. If it goes out of memory, it might kill some activities which are not being used. – Vucko Jun 29 '16 at 22:04
  • That's my question. Is it possible that the third Activity would come back, but somehow the first two would be gone, so I'd be "stranded" and be unable to press Back to go back to previous Activities? – user6419910 Jun 29 '16 at 22:06
  • @user6419910 yes its possible , you can disable back key in future activites or `moveTaskToBack` , or you can specify launch modes , you can read this page : [android manifest activity elements](https://developer.android.com/guide/topics/manifest/activity-element.html) – Yasin Kaçmaz Jun 29 '16 at 22:09
  • I'm not sure this addresses the situation I'm asking about – user6419910 Jun 29 '16 at 22:37

2 Answers2

0

The OS will handle how long a particular Activity stays "active" in memory. However, the "stack" shouldn't change, regardless of whether an activity is "active" or not. This is where the Bundle comes in handy and the methods: "onSaveInstanceState()" and "onRestoreInstanceState()".

Implementing these methods properly is the difference of the activity reappearing on the screen in an empty state vs. with its previous state maintained.

Some documentation on Recreating an Activity

FishStix
  • 4,994
  • 9
  • 38
  • 53
  • I am familiar with save and restore instance state, but how do I apply that to the situation I mentioned? – user6419910 Jun 29 '16 at 22:37
  • In my experience the stack is still "intact". As in the OS may have killed Activity 1 and 2, is displaying 3, then when you hit "back" it'll recreate activity 2 with the stored bundle. And then hitting back again will recreate activity 1 with its stored bundle – FishStix Jun 29 '16 at 22:48
  • When you say stored bundle, is this something you have to implement directly or does Android handle this? – user6419910 Jun 29 '16 at 22:52
  • This happens automatically if you implement the two methods mentioned. Android calls these methods with a bundle that can be filled with your data. Related link: http://stackoverflow.com/questions/6525698/how-to-use-onsavedinstancestate-example-please – FishStix Jun 29 '16 at 22:58
0

The backstack stays intact for the task in hand. It will always stay in task and whenever the user presses the back button, it will go through the back stack like popping the last item. However, not all the activities in the stack are in the foreground. Usually, only the last item put into the back stack (the top of the stack) is in the foreground and if there are multiple apps/tasks open this may not even be true. Here is a great diagram to show this. Activity back stack

Now, lets say a user opens a task with a couple of activities in its back stack. The top activity is in the foreground and is running normally, but to preserve memory the other activities were destroyed. So now when the user presses the back button, the task knows what activity was in the back stack and knows it is now destroyed. So, it will recreate it following the Activities lifecycle and any data that was in it will be lost. One way to preserve it (mentioned by original answer) is using the onSaveInstanceState() and onRestoreInstanceState(), which save things in a bundle that Android preserves for the user so data can be saved. All of this information can be found in the docs. To answer your question more clearly, yes you will be in the activity you think you would be in, but you can treat it as a new instance of that activity and to recover the data from before to display it in the same way, you should use bundle and implement the methods aforementioned.

Sahith Reddy
  • 263
  • 3
  • 10