lombrozo
lombrozo

Reputation: 169

Android app child activity not restored after press home/task-switcher

I have a main activity which creates a child activity when a button is pressed.

If I press the task-manager or home button when the child activity is displayed, the child activity is destroyed, and I get onSaveInstanceState then onDestroy called (for the child).

When I switch back to my app, the user is returned to the root activity and the child is not restored. The only notification I receive is the main activity onResume.

I've tried setting the alwaysRetainTaskState flag in my root activity, but doesn't help.

Can any one offer any advice?

<application
    android:name=".Application"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar"
        android:alwaysRetainTaskState="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".GameActivity"
        android:label="@string/app_name"
        android:noHistory="true">
        <!--android:theme="@style/AppDialogTheme"-->
    </activity>
</application>

public void onClickStart(View v) {
    ((Application) getApplication()).resetCurrentScore();
    ((Application) getApplication()).resetCurrentLevel();
    ((Application) getApplication()).resetLives();

    Integer level = ((Application) getApplication()).getCurrentLevel();

    if (level <= 3) {
        Intent GameActivity = new Intent(getApplicationContext(), GameActivity.class);
        startActivity(GameActivity);
    }
    else {
        Intent SpellGameActivity = new Intent(getApplicationContext(), SpellGameActivity.class);
        startActivity(SpellGameActivity);
    }
}

Upvotes: 0

Views: 172

Answers (1)

NoChinDeluxe
NoChinDeluxe

Reputation: 3444

What's likely causing this is your android:noHistory attribute in your GameActivity block. This causes the Activity to leave no stack trace, and for finish() to be called on it when it leaves the user's view, which is why onDestroy() would be called and why the Activity is destroyed.

From the docs:

android:noHistory

Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen — "true" if it should be finished, and "false" if not. The default value is "false". A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it. In this case, onActivityResult() is never called if you start another activity for a result from this activity.

Upvotes: 1

Related Questions