【发布时间】:2021-02-04 07:15:27
【问题描述】:
当我的导航图中有多个顶级目的地时,当我单击后退键完成我的应用程序时,startDestination 以外的那些目的地不会完成,而是弹出 startDestination 片段。
- FragmentA (startDestination) -> 使用返回键立即结束
- FragmentB -> 使用返回键返回 FragmentA
- FragmentC -> 使用返回键返回 FragmentA
我尝试过app:popUpTo="@id/nav_graph" 和/或app:popUpToInclusive="true",但没有成功。
活动:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(R.id.fragmentA, R.id.fragmentB, R.id.fragmentC)
.setOpenableLayout(drawerLayout)
.build();
NavigationView navView = findViewById(R.id.nav_view);
NavigationUI.setupWithNavController(navView, navController);
Toolbar toolbar = findViewById(R.id.toolbar);
NavigationUI.setupWithNavController(
toolbar,
navController,
appBarConfiguration
);
}
}
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Layout to contain contents of navigation_view body of screen (drawer will slide over this) -->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
<!-- Container for contents of drawer - use NavigationView to make configuration easier -->
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/navigation_view" />
</androidx.drawerlayout.widget.DrawerLayout>
nav_graph:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_graph"
app:startDestination="@id/fragmentA">
<fragment
android:id="@+id/fragmentA"
android:name="com.stackoverflow.FragmentA"
android:label="@string/fragmentA">
<argument
android:name="message"
android:defaultValue="(no message)"
app:argType="string" />
</fragment>
<action
android:id="@+id/toFragmentAWithMessage1"
app:destination="@id/fragmentA">
<argument
android:name="message"
android:defaultValue="message 1"
app:argType="string" />
</action>
<action
android:id="@+id/toFragmentAWithMessage2"
app:destination="@id/fragmentA">
<argument
android:name="message"
android:defaultValue="message 2"
app:argType="string" />
</action>
<fragment
android:id="@+id/fragmentB"
android:name="com.stackoverflow.navigationui.FragmentB"
android:label="@string/fragmentB">
<argument
android:name="message"
android:defaultValue="(no message)"
app:argType="string" />
</fragment>
<action
android:id="@+id/toFragmentBWithMessage1"
app:destination="@id/fragmentB">
<argument
android:name="message"
android:defaultValue="message 1"
app:argType="string" />
</action>
<action
android:id="@+id/toFragmentBWithMessage2"
app:destination="@id/fragmentB">
<argument
android:name="message"
android:defaultValue="message 2"
app:argType="string" />
</action>
<fragment
android:id="@+id/fragmentC"
android:name="com.stackoverflow.FragmentC"
android:label="@string/fragmentC">
<argument
android:name="message"
android:defaultValue="(no message)"
app:argType="string" />
</fragment>
<action
android:id="@+id/toFragmentCWithMessage1"
app:destination="@id/fragmentC">
<argument
android:name="message"
android:defaultValue="message 1"
app:argType="string" />
</action>
<action
android:id="@+id/toFragmentCWithMessage2"
app:destination="@id/fragmentC">
<argument
android:name="message"
android:defaultValue="message 2"
app:argType="string" />
</action>
</navigation>
我知道覆盖 OnBackPressedCallback 是一种解决方法。
public class FragmentB extends Fragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requireActivity().getOnBackPressedDispatcher().addCallback(new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
remove();
requireActivity().finish();
}
});
}
}
有没有更根本的解决方案?
【问题讨论】:
标签: android android-architecture-navigation android-navigation-graph