开发者

How to clear stack back to root activity when user leaves application?

I have an application with 2 activities, LogonAct and MainAct. LogonAct is a logon activity which I want to force 开发者_Python百科the user to go through each time they return to the application. I've set android:clearTaskOnLaunch="true" on LogonAct.

When I first start the app I go through this sequence of screens,

Home -> LogonAct -> MainAct -> Home

I then follow this sequence,

LogonAct -> Back -> MainAct

Why is it bringing me back to MainAct? Shouldn't that activity haven been closed since LogonAct has android:clearTaskOnLaunch="true". I expected to be brought back to Home when I hit the Back button from LogonAct.

Relevant snippets from AndroidManifest.xml,

   <activity android:name=".LogonAct"
             android:clearTaskOnLaunch="true">
       <intent-filter>
           <action android:name="android.intent.action.MAIN"/>
           <category android:name="android.intent.category.LAUNCHER"/>
       </intent-filter>
   </activity>

   <activity android:name=".MainAct">
       <meta-data android:name="android.app.default_searchable"
                  android:value=".SearchResults" />
   </activity>

I'm using 1.5.

Any help appreciated.


You can do following:
1. set clearTaskOnLaunch = "true" in AndroidManifest, in declaration of main activity
2. in activity that must close:

@Override
public void onBackPressed(){
    moveTaskToBack(true);
}

so if user presses back - it comes back to homescreen if user launches aplication again - task stack clears and he comes to root main activity


The docs for android:clearTaskOnLaunch mention that this attribute applies "whenever [the Activity] is re-launched from the home screen".

However, in your case you're pressing the Home button to return to the Home screen, rather than pressing the Back button. This means your application isn't actually relaunched because the MainAct was not "finished". That only happens when you press Back (or if Android kills the task to save resources etc.).

As you only have two activities in your application, you could set the android:noHistory attribute on MainAct, thus ensuring that users can never return to it and must pass through the LogonAct.

As an aside, it seems a bit annoying to force users to re-login every time they navigate away from the app (for example when they receive a phone call).
You could retain a session token with timeout in your app's persistent storage, or hold a network connection open using a service if that's how your app works — but of course that's up to you and your requirements. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜