onCreate(Bundle savedInstanceState) in always null
Asked Answered
G

4

6

I know, this question is asked before on stackoverflow, but non of the answers worked for me.

Probably worth mentioning:

  • I use ActionBarSherlock with the support package.
  • Method onSaveInstanceState IS called when I press the home button. The method onCreate just always gives NULL for the Bundle savedInstanceState.
  • Method onRestoreInstanceState is never called at all. (I wouldn't mind if the onCreate worked ;)).
  • Also (it shouldn't matter) I tried putting super.onSaveInstanceState(outState) at the bottom of onSaveInstanceState. No luck either.

Here's the code. I hope someone had this problem and solved it.

public class MainActivity extends SherlockFragmentActivity {

    private static final String LOG_TAG = MainActivity.class.getSimpleName();

    private static String STATE_TO_STORE = "state_to_store";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        

        Log.d(LOG_TAG, "onCreate: savedInstanceState = " + (savedInstanceState == null ? "NULL" : "Not NULL"));

        // ... more code...
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        Log.d(LOG_TAG, "onRestoreInstanceState: savedInstanceState = " + (savedInstanceState == null ? "NULL" : "Not NULL"));
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt(STATE_TO_STORE, 5); // store some int

        Log.d(LOG_TAG, "onSaveInstanceState bundle: " + outState.toString());
    }

    // ... more code ...

}

The logging clearly states onSaveInstanceState is being called and onCreate gets savedInstanceState = NULL.

Goodale answered 28/3, 2013 at 16:20 Comment(0)
M
12

Check that your manifest does not contain android:noHistory="true".

I spent hours looking for an answer and it was that simple.

Miles answered 7/5, 2013 at 21:13 Comment(1)
What if I still want noHistory to be true?Rill
P
3

In my case, the reason was that the specific activity did not have a theme declared in the manifest file.

To fix this, open AndroidManifest.xml, click Application, select the crashing activity in Application Nodes and add the theme in the Theme field of Attributes. In my case, it was

@style/Theme.AppCompat.Light.DarkActionBar

but you could copy the theme from one of your other activities.

P.S.: I know this is an answer to an old question, but I've stumbled upon it while searching for a fix and didn't find a working solution so this might help others.

Porras answered 5/2, 2014 at 21:1 Comment(0)
M
2

onRestoreInstanceState (or saved bundle in onCreate) will be fired when the Activity was killed by the system due to lack of resources and restarted when you get back to it. The Activity might not be killed (just stopped) and restarted without going through onRestoreInstanceState. Another words, onSaveInstanceState will be always called, but onRestoreInstanceState will be called if it is killed and restored by the system. Not just stopped and restarted, not paused and resumed and not started by a new intent.

Check my explanation here. I'm sure it covers your question.

when is onRestoreInstanceState called?

Missal answered 28/3, 2013 at 16:30 Comment(4)
Looks good, but how can I test this? How can I trigger an application kill/stop by the system? Everything I tried on the android phone myself didn't trigger onRestoreInstanceState. Also, if this works, how should I save the state if the user stops/kills this app him/herself? It looks to me I can only use SharedPreferences then...Goodale
onSaveInstanceState is used to save and restore the state that was when interrupted by system (opened dialogs, list position, etc). If you want to save some of your own state that should be preserved when you close the app or reboot, you should go for SharedPreferences and save the state in onPause().Missal
@DoctororDrive Your Activity might get called twice, so that you have one instance running in the background, ready to be restored at a later time, and one active in the foreground. Like when you have an Activity which also responds to android.intent.action.CREATE_SHORTCUT. In that case the Activity for the shortcut creation is running in the foreground, and will get destroyed before the background one comes to the foreground again, which can be several positions backwards in the history stack. In that case the shortcut creation "mode" will have overwritten the old one's shared preferences.Lightweight
While this is an old question, there is a "developer option" on Android devices or simulators called "Don't keep activities". Just activate it, then click the "square" button to leave your app and come back to your app then by clicking on the thumbnail of it. The app should have gone through a save/restore cycle thanks to the option (you can check with breakpoints).Senhauser
R
2

When you press the HOME button, your activity is pause and not destroyed Thus when you launch the app again from home screen, onCreate is not called, unless the OS kill your app to reclaim memory. If you want your activity to be recreated when launch from the home screen, put this line android:finishOnTaskLaunch="true" in the activity manifest.

Riotous answered 28/3, 2013 at 16:34 Comment(2)
Probably had to mention it more clearly, but I do have the onCreate called (just always returns null) since I stopped the app in the task manager by sliding it to the right. In my logs I see onCreate IS being called when I start the app again.Goodale
If you kill your app then I do not think the OS would keep the state of your acitivity anymore. But try android:alwaysRetainTaskState="true" and see if it works.Riotous

© 2022 - 2024 — McMap. All rights reserved.