android:alwaysRetainTaskState = false not being re

2019-08-11 07:08发布

问题:

I would like my app to exhibit the default behaviour described for android:alwaysRetainTaskState in the Android documentation:

Normally, the system clears a task (removes all activities from the stack above the root activity) in certain situations when the user re-selects that task from the home screen. Typically, this is done if the user hasn't visited the task for a certain amount of time, such as 30 minutes.

This isn't what I'm seeing. Even after >1 day, re-starting my app using the launcher icon returns the user to the place where they left it. For example, after a fresh install my app displays a home screen activity H when launched. The user then navigates to detail activities: H -> J. On relaunching after a long time, I would like the user to see H, but instead they see J.

These are the flags set on my activity in the AndroidManifest.xml:

<activity
    android:name=".AppHomeScreen"
    android:label="@string/app_name"
    android:alwaysRetainTaskState="false"
    android:launchMode="singleTop"
    android:windowSoftInputMode="stateUnchanged">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

I am running Android KitKat 4.4.2.

Is there any reason why I might not be seeing the expected behaviour here? I know that I could set android:clearTaskOnLaunch or android:finishOnTaskLaunch to clear the task every time the user leaves the app, but this is too aggressive, I'd like the described behaviour where the state is only forgotten after a long period of inactivity.

(The Android documentation doesn't seem to guarantee the behaviour, only that the task is cleared in "certain situations" and "after a certain amount of time, such 30 minutes". Maybe the default behaviour was changed and the Android docs are out of date?)

回答1:

This behaviour is manufacturer/vendor-specific. On different devices you will see different behaviour. There are devices that aggressively clear out tasks and others that retain task state for much longer periods of time.