gpt4 book ai didi

Android导航组件从homefragment关闭应用程序

转载 作者:行者123 更新时间:2023-12-02 12:47:09 26 4
gpt4 key购买 nike

如何在到达 HomeFragment(startDestination)时清除所有堆栈,当用户通过导航组件按下 homeFragment 时,我想关闭应用程序。

 <navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
app:startDestination="@+id/homeFragment">


<fragment android:id="@+id/homeFragment"
android:name="ui.fragment.home.HomeFragment"
tools:layout="@layout/fragmet_home"
android:label="HomeFragment"
app:launchSingleTop="true"
app:popUpToInclusive="true">

<action android:id="@+id/action_homeFragment_to_notificationFragment"
app:destination="@id/notificationFragment"
app:popUpTo="@+id/homeFragment"
app:enterAnim="@anim/pop_enter"
app:exitAnim="@anim/pop_exit"
app:popEnterAnim="@anim/right_in"
app:popExitAnim="@anim/right_out"/>
</Fragment>

<fragment android:id="@+id/notificationFragment"
android:name="ui.fragment.notification.NotificationFragment"
tools:layout="@layout/fragment_notification"/>
</navigation>

最佳答案

 @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

OnBackPressedCallback callback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
getActivity().finishAndRemoveTask();
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this,callback);
}

关于Android导航组件从homefragment关闭应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57087178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com