简体   繁体   中英

Android activity recreation after system kills it

I have a problem with a particular scenario with my app. The activtiy flow is following:

  1. SplashScreenActivity (this is my default launcher activity which does some important preloading of external application theme
  2. When my theme is loaded it automatically goes to next LoginActivity

The problem that is happening is when I leave my app in LoginActivity and put app in background by hitting the home button. In the background I start as many other apps as I can so that I can get phone memory in a critical state and in that way to force Android system to kill my activity in order for the other apps to use this memory.

When this scenario occurs I try to get back to my app that is still in the list of opened apps and when I do that I've read that Android system recreates the last activity that was opened when this memory release occured and that is the moment when my app crashes because it doesn't have some data that was stored in memory.

My question is, is there any way that I can tell or configure the app that when this scenario happens to NOT recreate last activity but to launch my default launcher activity (in this case SplashScreenActivity ) which would normally preload all the necessary data that is used later on in the LoginActivity.

Note I don't need my app to start the SplashScreenActivity every time uses puts application in a background and then goes back to foreground. I need it launch this default activity only when Android system kill's it because it requires some memory.

You could check if the required ressources are still available. If they're not you could go back to the first activity by using

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

This will remove all activies from the stack and you can start from the beginning.

For easier debugging you could also activate the "Don't keep activities" option in the developer settings. This will always destroy the activity when you go out of the app.

If your data can be stored with the onSaveInstanceState method I would recommend doing so. Although I understand the need to do it otherwise as the android method to save the activity state is quite limited when it comes to complex objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM