2

I have an Activity that call another one by calling startActivity(), after some time onSaveInstanceState(Bundle outState) is called and I set a Boolean valeu to recover on the onCreate(Bundle savedInstanceState) but when this method is called savedInstanceState comes null. I've searched the internet for an answer, but didn't find anything for my case.

Here's the onSaveInstanceState method:

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState = new Bundle();
        outState.putBoolean(Constants.MAIN_ACTIVITY_STATE_RECREATED, true);
        super.onSaveInstanceState(outState);
    }

The onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setTitle(R.string.activity_main);
    if (savedInstanceState!=null && savedInstanceState.getBoolean(Constants.MAIN_ACTIVITY_STATE_RECREATED, false)){
        return;
    }
    //DO SOME STUFF
}

And here's my AndroidManifest declaration of the Activities:

   <activity android:name=".MainActivity"
              android:screenOrientation="portrait"
              android:configChanges="orientation|keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

This is the called Activity:

       <activity android:name=".PhotoUserActivity"
                  android:screenOrientation="portrait"
                  android:configChanges="orientation|keyboardHidden"
            android:parentActivityName=".MainActivity">
        </activity>

If I remove parentActivityName from the declaration of the second activity and make the "back button" by myself, the onCreate of the first Activity is never called.

Thanks for any help!

Elvis Oliveira
  • 941
  • 2
  • 15
  • 29

2 Answers2

3

You do not need to initialize the outState variable otherwise you wont be able to pass the vallue.

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        //outState = new Bundle(); <-- Remove this
        super.onSaveInstanceState(outState);
        outState.putBoolean(Constants.MAIN_ACTIVITY_STATE_RECREATED, true);
    }

The outState Bundle is provided by the framework, by initializing it e.g. new Bundle() you are potentially dropping some value on that previous Bundle if it contains any.

It is always advisable to call the super.onSaveInstanceState(Bundle outState) first before adding new value.

Enzokie
  • 7,365
  • 6
  • 33
  • 39
1

If you save the state of the application in a bundle (typically non-persistent, dynamic data in onSaveInstanceState), it can be passed back to onCreate if the activity needs to be recreated (e.g., orientation change) so that you don't lose this prior information. If no data was supplied, savedInstanceState is null.

... you should use the onPause() method to write any persistent data (such as user edits) to storage. In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created. See the Process Lifecycle section for more information on how the lifecycle of a process is tied to the activities it is hosting. Note that it is important to save persistent data in onPause() instead of onSaveInstanceState(Bundle) because the latter is not part of the lifecycle callbacks, so will not be called in every situation as described in its documentation.

source

Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60