简体   繁体   English

导航组件无法弹出以退出应用程序

[英]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?对于这种行为,我真的需要覆盖onBackPressedonBackPressed活动吗? 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.

相关问题 检测导航组件中的弹出窗口 - Detect pop in Navigation Component 当用户在导航组件中按下返回按钮时如何退出应用程序? - How to exit the app when the user press back button in navigation component? 无法使用导航和喷气背包组合弹出起始目的地 - Can't pop starting destination using navigation and jetpack compose 使用导航组件从应用程序注销时如何退出应用程序? - How to exit app from login while logout from the app using navigation component? 导航组件弹出行为无法正常工作 - Navigation component pop behavior is not working as it should 组件导航,从带参数的 backstack 弹出 - Component Navigation , pop from backstack with arguments Android导航组件弹出过渡问题 - Android Navigation Component pop to transition issue 弹出输入 animation 不适用于 Jetpack 导航组件 - Pop enter animation not working with the Jetpack Navigation Component 我无法使用 Flutter 中的后退按钮退出应用程序 - I can't exit app with back button in Flutter 带有导航组件和导航抽屉的单一活动应用程序 - Single activity app with Navigation Component and Navigation Drawer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM