且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何清除栈回到根系活力,当用户离开应用程序?

更新时间:2023-12-01 20:02:10

该文档的安卓clearTaskOnLaunch 提到,这个属性应用于每当[活动]为重从主屏幕上。

推出

不过,你的情况,你是pressing的首页骨节病>按钮返回主屏幕,而不是pressing的返回骨节病>按钮。这意味着你的应用程序实际上不是的重新启动的,因为 MainAct 并没有完成。这仅发生在你preSS 返回(或者安卓杀死任务,节约资源等)。

因为你只需要在应用程序中的两项活动,您可以设置android:noHistory$c$c>属性对 MainAct ,从而确保用户永远不能恢复到它,必须通过 LogonAct

顺便说一句,似乎有点恼人,迫使用户重新登录每次他们从应用程序导航离开时间(例如,当他们收到电话)。
你可以保持与超时会话令牌在你的应用程序的永久存储,或保持网络连接打开使用服务,如果这是如何您的应用程序的工作原理—但当然,这是给你和你的要求。 :)

I have an application with 2 activities, LogonAct and MainAct. LogonAct is a logon activity which I want to force 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.

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. :)