[英]Navigation Component can't pop to exit app
Using Android's Navigation Component
, I have 3 fragments hosted by a single Activity.使用 Android 的
Navigation Component
,我有 3 个由单个 Activity 托管的片段。 The launch fragment is a splash screen fragment (A), if the user is not logged in, I launch the login fragment (B), if they are logged in, I launch a list fragment (C).启动片段是启动画面片段 (A),如果用户未登录,则启动登录片段 (B),如果已登录,则启动列表片段 (C)。
So launch routes are either A->B->C or A->C.所以发射路线要么是A->B->C,要么是A->C。
When you land on B or C, pressing back should kill the app.当你落在 B 或 C 上时,按回应该会杀死应用程序。 The NavigationController though is instead backs up to A (I think, A's
onActivityCreated
is certainly called at which point it crashes which is probably unrelated).而 NavigationController 反而备份到 A (我认为,A 的
onActivityCreated
肯定会被调用,此时它崩溃了,这可能是无关的)。
Pop behaviors in the graph editor for A -> B seem to allow me to pop to different fragments but there doesn't seem to be an option to just kill the app.图形编辑器中 A -> B 的弹出行为似乎允许我弹出不同的片段,但似乎没有一个选项可以杀死应用程序。
Do I really need to override onBackPressed
for this behavior and just kill the activity?对于这种行为,我真的需要覆盖
onBackPressed
并onBackPressed
活动吗? Because this is easier without the NavigationController, usually I would just finish an activity as I start a new one.因为没有 NavigationController 这会更容易,所以通常我会在开始一个新活动时完成一个活动。
Open text tab in Graph Editor to view xml code, find your two actions A -> B and A -> C and put tag: app:clearTask="true" , it's should kill you app when user press back button.在图表编辑器中打开文本选项卡以查看 xml 代码,找到您的两个操作 A -> B 和 A -> C 并放置标签: app:clearTask="true" ,当用户按下后退按钮时,它应该会杀死您的应用程序。
Example:例子:
<fragment
android:id="@+id/launcher_fragment"
android:name="com.example.LauncherFragment"
android:label="launcher_fragment">
<action
android:id="@+id/action_launcher_to_login"
app:destination="@id/login_fragment"
app:clearTask="true"/>
<action
android:id="@+id/action_launcher_to_list"
app:destination="@id/list_fragment"
app:clearTask="true" />
</fragment>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.