Android 用户离开应用程序时如何将堆栈清除回根活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2131123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:40:09  来源:igfitidea点击:

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

androidandroid-activity

提问by Adrian

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.

我有一个包含 2 个活动的应用程序,LogonAct 和 MainAct。LogonAct 是一个登录活动,我想强制用户在每次返回应用程序时都通过它。我在LogonAct 上设置了android:clearTaskOnLaunch="true"

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

当我第一次启动应用程序时,我会浏览这一系列的屏幕,

Home -> LogonAct -> MainAct -> Home

Home -> LogonAct -> MainAct -> Home

I then follow this sequence,

然后我按照这个顺序,

LogonAct -> Back -> MainAct

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.

为什么它让我回到 MainAct?自 LogonAct 具有android:clearTaskOnLaunch="true"以来,该活动不应该关闭。当我点击 LogonAct 的“返回”按钮时,我预计会被带回主页。

Relevant snippets from AndroidManifest.xml,

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

我正在使用 1.5。

Any help appreciated.

任何帮助表示赞赏。

采纳答案by Christopher Orr

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

android:clearTaskOnLaunch提到此属性适用于“每当 [活动] 从主屏幕重新启动时”的文档。

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

但是,在您的情况下,您是按下Home按钮返回主屏幕,而不是按下Back按钮。这意味着您的应用程序实际上并未重新启动,因为MainAct尚未“完成”。只有当您按下时才会发生这种情况Back(或者如果 Android 终止任务以节省资源等)。

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

由于您的应用程序中只有两个活动,您可以将android:noHistory属性设置为MainAct,从而确保用户永远不会返回它并且必须通过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. :)

顺便说一句,强迫用户每次离开应用程序时都重新登录似乎有点烦人(例如,当他们接到电话时)。
您可以在您的应用程序的持久存储中保留一个带有超时的会话令牌,或者如果您的应用程序是这样工作的,则使用服务保持网络连接打开 - 当然这取决于您和您的要求。:)

回答by Arseniy

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

您可以执行以下操作:
1. 在 AndroidManifest 中设置 clearTaskOnLaunch = "true",在主要活动的声明中
2. 在必须关闭的活动中:

@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

因此,如果用户按回 - 如果用户再次启动应用程序,它会返回到主屏幕 - 任务堆栈清除并且他进入根主活动