android - 在 AndroidX FragmentContainerView 中处理 Fragment 的 BackPress
问题描述
我有一个MainActivity,我在其中实现了一个抽屉和一个片段。现在,我正在使用下面提到的ManageFragments类从堆栈中添加和删除片段。在Back Press上添加到以前的片段后,我能够将片段替换为另一个片段,但无法从堆栈中弹出片段。到目前为止,这是我的实现。
这是我想要实现的:
DrawerItem1 -> Frag1 -> Frag2 -> Frag3 -> Frag4
DrawerItem2 -> Frag5
DrawerItem3 -> Frag6
DrawerItem4 -> Frag7
因此,当用户在 frag1 中并按下 Frag5 时。用户应该能够返回 frag1。
同样,如果用户导航到 frag4,则可以通过后按导航回frag1
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
我开发了一个通用类,我可以在其中添加和弹出片段,即“nav_host_fragment”
管理片段.java
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import java.util.Objects;
public class ManageFragments {
public static String backStateName;
public static String fragmentTag;
public static FragmentManager manager;
public static boolean fragmentPopped;
public static void replaceFragment(FragmentActivity fragmentActivity, Fragment fragment) {
backStateName = fragment.getClass().getName();
fragmentTag = backStateName;
manager = fragmentActivity.getSupportFragmentManager();
fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
if (!fragmentPopped && manager.findFragmentByTag(fragmentTag) == null) { //fragment not in back stack, create it.
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.nav_host_fragment, fragment, fragmentTag);
ft.addToBackStack(backStateName);
ft.commit();
}
}
public static void popFragment(FragmentActivity fragmentActivity) {
manager = Objects.requireNonNull(fragmentActivity).getSupportFragmentManager();
manager.popBackStackImmediate(backStateName, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
public static void clearFragments(FragmentActivity fragmentActivity) {
manager = Objects.requireNonNull(fragmentActivity).getSupportFragmentManager();
for (Fragment fragment : manager.getFragments()) {
fragmentActivity.getSupportFragmentManager().beginTransaction().remove(fragment).commit();
}
}
}
这是我的MainActivity.java实现。
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private DrawerLayout drawer;
private NavController navController;
private NavHostFragment navHostFragment;
private NavigationView navigationView;
public static Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
drawer = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
navHostFragment =
(NavHostFragment) getSupportFragmentManager()
.findFragmentById(R.id.nav_host_fragment);
navController = navHostFragment.getNavController();
mAppBarConfiguration = new AppBarConfiguration.Builder(
navController.getGraph())
.setDrawerLayout(drawer)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_category, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController, mAppBarConfiguration);
}
@Override
public void onBackPressed() {
}
}
mobile_navigation.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@+id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.drawerapp.fragment.home.HomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home">
<action
android:id="@+id/action_nav_home_to_subCategoryFragment"
app:destination="@id/subCategoryFragment"
app:enterAnim="@anim/fragment_fade_enter" />
</fragment>
<fragment
android:id="@+id/subCategoryFragment"
android:name="com.drawerapp.fragment.sub_category.SubCategoryFragment"
android:label="SubCategoryFragment">
<action
android:id="@+id/action_subCategoryFragment_to_galleryFragment"
app:destination="@id/galleryFragment"
app:enterAnim="@anim/fragment_fade_enter" />
<action
android:id="@+id/action_subCategoryFragment_to_nav_home"
app:destination="@id/nav_home"
app:exitAnim="@anim/fragment_fade_exit" />
</fragment>
<fragment
android:id="@+id/galleryFragment"
android:name="com.drawerapp.fragment.gallery.GalleryFragment"
android:label="GalleryFragment">
<action
android:id="@+id/action_galleryFragment_to_subCategoryFragment"
app:destination="@id/subCategoryFragment"
app:exitAnim="@anim/fragment_fade_exit" />
</fragment>
<fragment
android:id="@+id/nav_fav"
android:name="com.drawerapp.fragment.favorites.FavoritesFragment"
android:label="@string/menu_fav"
tools:layout="@layout/fragment_favorite">
<action
android:id="@+id/action_nav_fav_to_nav_home"
app:destination="@id/nav_home"
app:exitAnim="@anim/fragment_fade_exit" />
</fragment>
<fragment
android:id="@+id/nav_feedback"
android:name="com.drawerapp.fragment.feedback.FeedbackFragment"
android:label="@string/menu_feedback"
tools:layout="@layout/fragment_feedback">
<action
android:id="@+id/action_nav_feedback_to_nav_fav"
app:destination="@id/nav_fav"
app:exitAnim="@anim/fragment_fade_exit" />
</fragment>
<fragment
android:id="@+id/nav_about_us"
android:name="com.drawerapp.fragment.about_us.AboutUsFragment"
android:label="@string/menu_about_us"
tools:layout="@layout/fragment_about_us">
<action
android:id="@+id/action_nav_about_us_to_nav_feedback"
app:destination="@id/nav_feedback"
app:exitAnim="@anim/fragment_fade_exit" />
</fragment>
先感谢您。
解决方案
您正在使用NavHostFragment
,因此您根本不应该进行任何片段事务。
您尝试实现的行为正是您将 NavController 连接到 DrawerLayout时的默认行为,因此您应该删除所有这些代码并遵循文档。
推荐阅读
- python - Python 代码仅在 Linux 集群中存在内存问题/在 Windows 桌面 PC 中正常
- javascript - 控制台记录十进制步数以在 javascript 中编号
- scip - 有没有办法静态编译libscip?
- angular - 在另一个组件中使用 @output 值
- tensorflow - GNN keras:ConcatOp:输入的尺寸应该匹配
- selenium - 如果我们无权使用任何外部包生成报告,我们也不能使用 testng 生成的 html 报告,如何生成报告
- python - 如何只添加在线会员telethon?
- bash - 如何在 makefile 先决条件中使用大括号扩展?
- javascript - Angular从子组件调用父组件函数,从sessionStorage实时更新变量
- css - 导航栏菜单中的列表项是否可以有不同的对齐方式?