SBotirov
SBotirov

Reputation: 14138

Android: Activity onCreate savedInstanceState allways is null

So, I know is question is duplicated, but I didn't find solution in these answers. I have a MainActivity and GameDetailActivity. MainActivity hierarchical parent of GameDetailActivity. Here it is declarations in manifest file:

    <activity
        android:name=".MainActivity"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme2"
        android:noHistory="false"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name=".GameDetailActivity"
        android:theme="@style/AppTheme2"
        android:parentActivityName=".MainActivity"
        android:noHistory="false"
        >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="de.exam.example.MainActivity"/>
    </activity>

here AppTheme2 declaration:

<style name="AppTheme2" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

here back logic in GameDetailActivity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                TaskStackBuilder.create(this)
                        .addNextIntentWithParentStack(upIntent)
                        .startActivities();
            } else {
                NavUtils.navigateUpTo(this, upIntent);
            }
            return true;
        default:break;
    }
    return super.onOptionsItemSelected(item);
}

and when I press home up (back "<-") button in GameDetailActivity I am getting always savedInstanceState is null in MainActivity. I don't understand why saved data lost in MainActivity.

EDIT

Here start GameDetailActivity code:

Intent i = new Intent(getActivity(), GameDetailActivity.class);
i.putExtra("game", clickedGame);
startActivity(i);

Anybody know how to fix this?

Thank you in advance

Upvotes: 0

Views: 415

Answers (3)

earthw0rmjim
earthw0rmjim

Reputation: 19417

The savedInstanceState bundle is getting saved in onSaveInstanceState().

onSaveInstanceState() is called when the OS destroys the Activity (to reclaim resources for example, or because of a configuration change), it is not guaranteed to be called by simply navigating through activities.

Upvotes: 1

Sohail Zahid
Sohail Zahid

Reputation: 8149

Navigate up with a new back stack

When user presses the Up button after entering your activity from another app's task, your app starts a new task with the appropriate back stack before navigating up.

Solution

Navigate Up to Parent Activity

When you call this method, it finishes the current activity and starts (or resumes) the appropriate parent activity. If the target parent activity is in the task's back stack, it is brought forward. The way it is brought forward depends on whether the parent activity is able to handle an onNewIntent() call.

Source


   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

Upvotes: 1

Vijay Kumar M
Vijay Kumar M

Reputation: 182

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent. May this will solve your problem

public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()== android.R.id.home) {
        Intent intent = NavUtils.getParentActivityIntent(this);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        NavUtils.navigateUpTo(this, intent);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Upvotes: 1

Related Questions