0

Can somebody clarify please why I have such a weird behavior. Up to documentation the Bundle savedInstanceState which is set in onSaveInstanceState() is alive as long as application alive, so when it it in foreground or background. After the application is being killed the savedInstanceState instance is killed as well. Here is what I have:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (savedInstanceState != null) {
            Log.i("Dev", "not null");
        } else {
            Log.i("Dev", "null");
        }
    }

Here is how I set it:

@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("bool", true);
    }

Then, I'm starting the application in the emulator. After application is opened I click home button so the Launcher is visible. Then I kill the application's process using adb. After that I start the application from the list of recently used application expecting for "null" in the Logcat, but what I actually see is "not null", so my understanding is incorrect?

Eugene
  • 59,186
  • 91
  • 226
  • 333
  • the documentaion says If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). – Blackbelt May 30 '13 at 15:30
  • So I can rely the information I put there is still available after the application is being killed? – Eugene May 30 '13 at 15:49
  • imo it is unclear. Sometimes the documetation is really misleading. – Blackbelt May 30 '13 at 15:51
  • Maybe it is a bug. Have you checked if somebody filled up something about in the android bugtracker? – Blackbelt May 30 '13 at 16:00
  • you are welcome. Could you update this question as soon as you have information about? – Blackbelt May 30 '13 at 16:02
  • I answered this in your other question: http://stackoverflow.com/questions/16837595/will-bundle-savedinstancestate-be-alive-after-application-is-being-killed – David Wasser Jun 04 '13 at 21:07

2 Answers2

0

Isn't it very clearly stated here ? Or do I missunderstand your question?

[..]To save additional data about the activity state, you must override the onSaveInstanceState() callback method. The system calls this method when the user is leaving your activity and passes it the Bundle object that will be saved in the event that your activity is destroyed unexpectedly. If the system must recreate the activity instance later, it passes the same Bundle object to both the onRestoreInstanceState() and onCreate() methods.

Saved Instance Life cycle

I mean for me this is also reasonable in most situations. Because when your activity / app is in the background and the android system closes it (let's say because it needs more memory), then it first saves the state. So next time the users opens your activity, you can restore it's previous state (and that may also exactly be what the user wants, since it wasn't him who closed the activity, but the system itself).

OschtärEi
  • 2,255
  • 3
  • 20
  • 41
  • well I think `onDestroyed`is also called when terminating the app like that? – OschtärEi Jun 03 '13 at 06:46
  • @OschtärEi No. `onDestroy()` is not called if the process hosting the application is killed. No lifecycle methods are called, it all just goes away. – David Wasser Jun 04 '13 at 21:04
0

The Bundle is saved for as long as Android wants it to be saved/can save it. One of the "features" (quotes because it ends up being a bad idea as often as it is a good one) of Android is that applications are never exited (to the user's view). Their mechanism of doing this is the onSaveInstanceState- it stores the Bundle, and when the app is later reinitialized by some method (such as from the recent activities menu) it will pass that Bundle to the onCreate and let it re-initialize itself.

Of course this also causes problems. For example, if you save login info, exiting an application won't log you out. So a user can then just hand his phone to a friend to watch a video, thinking that he exited his mobile banking app and is safe, yet the friend can call it back up and recreate it. If your app has large data structures in static variables or singletons they will not be recreated unless you code it carefully. Apps that require activities to be explored in order can be restarted from the middle.

Now Android can choose to forget your Bundle. If you put several MB in it, I would expect android to forget it rapidly. But it will remember it for as long as it can.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127