5

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.

user20902
  • 157
  • 1
  • 10
  • Welcome to Android. Take care about the Activity lifecycle http://developer.android.com/reference/android/app/Activity.html – Stefano Vuerich May 19 '15 at 14:01
  • Have you tried looking at [onSaveInstanceState](http://stackoverflow.com/a/151940/4896787) at all? You should be able to use this method, along with onCreate to solve your problem – Joseph Roque May 19 '15 at 14:02

1 Answers1

2

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.

davidgiga1993
  • 2,695
  • 18
  • 30
  • Can I rely that savedInstanceState variable will be null the first time activity is created and not null when it is recreated ? Tried it several times and it always goes this way, is there some exception in which this savedInstanceState variable will be not null value the first time activity is created ? – user20902 May 20 '15 at 06:14
  • 1
    savedInstanceState() will only be called when the activity gets destroyed by the system. Take a look at: http://developer.android.com/training/basics/activity-lifecycle/recreating.html the savedInstanceState variable will be null when the activity is first created – davidgiga1993 May 20 '15 at 07:09