0

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

SBotirov
  • 13,872
  • 7
  • 59
  • 81

3 Answers3

1

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);
    }
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
1

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);
}
1

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.

earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
  • Do you recommend use other variants such as SharedPreference, static fields in application class? – SBotirov Nov 07 '16 at 06:46
  • It's hard to suggest a solution without knowing exactly what you're trying to achieve. `SharedPreferences` could be a possible solution to persist the data. If you would like to use the data in `GameDetailActivity` also, you could just pass it in the `Intent` as an extra. – earthw0rmjim Nov 07 '16 at 06:50