0

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?

Anindya Dutta
  • 1,972
  • 2
  • 18
  • 33
Mehdi
  • 661
  • 5
  • 17

3 Answers3

0

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

Kartheek
  • 7,104
  • 3
  • 30
  • 44
0

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.

Satya Ch
  • 29
  • 4
0

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!

Mehdi
  • 661
  • 5
  • 17