I developed an Android app recently. Everything goes well until I press home key on my devices. On one of my devices (which has more memory), activity resumes in right state and my app runs well. But on other device, activity kills after some seconds and when I resume app it restarts the activity that I left with default values (incorrect behavior). How can I force app to restart app from main activity instead of restarting this activity with default values?
-
Can you post your `onPause()` and `onDestroy()` code for that Activity? – Anirudh May 22 '15 at 11:32
-
on onPause() , it shows a dialog only, and onDestroy() is not implemented. – Mehdi May 22 '15 at 11:34
-
In your onPause() you are going to have to save your values (somewhere like shared preferences), and in the onCreate() just check what the sharedPreferences are and load those?. – Smashing May 22 '15 at 11:39
-
Yeah, this is a way, but hard to achieve, because I have more than 50 values to save and restore, i just want prevent from load this activity, I want to start app from main activity! – Mehdi May 22 '15 at 11:41
-
`How can I force app to restart app from main activity instead of restart this activity with default values?` what does this mean ? – Murtaza Khursheed Hussain May 22 '15 at 11:53
-
it seems that you looking for a `LaunchMode` in activity tag in manifest – Murtaza Khursheed Hussain May 22 '15 at 11:56
-
I mean restart app from main activity instead of activity that I left – Mehdi May 22 '15 at 11:57
3 Answers
try to set android:alwaysRetainTaskState="true"
for your activity in your Manifest.xml
According to the documentation
Whether or not the state of the task that the activity is in will always be maintained by the system — "true" if it will be, and "false" if the system is allowed to reset the task to its initial state in certain situations. The default value is "false". This attribute is meaningful only for the root activity of a task; it's ignored for all other activities. Normally, the system clears a task (removes all activities from the stack above the root activity) in certain situations when the user re-selects that task from the home screen. Typically, this is done if the user hasn't visited the task for a certain amount of time, such as 30 minutes.
However, when this attribute is "true", users will always return to the task in its last state, regardless of how they get there. This is useful, for example, in an application like the web browser where there is a lot of state (such as multiple open tabs) that users would not like to lose.
for more reference click here

- 7,104
- 3
- 30
- 44
If you use android:clearTaskOnLaunch="true", complete backstack will be cleared. Your application will be freshly started from beginning, that means applications main activity will be launched and if you don't store the data, you will end up with default values.

- 29
- 4
Finally I got the answer:
We have to set a global static variable in main activity in onCreate() and finish if that variable had been reset to null in activity that we left the app!

- 661
- 5
- 17