【Android】在MainActivity 中跳转Fragment页面

【Android】在MainActivity 中跳转Fragment页面

前提

我的底部导航组件使用

xml 复制代码
<com.google.android.material.bottomnavigation.BottomNavigationView  
    android:id="@+id/nav_view"  
    android:layout_width="0dp"  
    android:layout_height="wrap_content"  
    app:layout_constraintHorizontal_bias="0.0"  
    android:layout_marginStart="0dp"  
    android:layout_marginEnd="0dp"  
    android:background="?android:attr/windowBackground"  
    app:labelVisibilityMode="labeled"  
    app:layout_constraintBottom_toBottomOf="parent"  
    app:layout_constraintLeft_toLeftOf="parent"  
    app:layout_constraintRight_toRightOf="parent"  
    app:menu="@menu/bottom_tab_bar_menu" 
/>  
<fragment  
    android:id="@+id/fragment_container"  
    android:name="androidx.navigation.fragment.NavHostFragment"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    app:defaultNavHost="true"  
    app:layout_constraintBottom_toTopOf="@id/nav_view"  
    app:layout_constraintHorizontal_bias="0.0"  
    app:layout_constraintLeft_toLeftOf="parent"  
    app:layout_constraintRight_toRightOf="parent"  
    app:layout_constraintTop_toTopOf="parent"  
    app:layout_constraintVertical_bias="0.0"  
    app:navGraph="@navigation/mobile_navigation"
/>

跳转使用

java 复制代码
BottomNavigationView navView = findViewById(R.id.nav_view); 
navController = Navigation.findNavController(this, R.id.fragment_container); 
NavigationUI.setupWithNavController(navView, navController);

需求说明

假设我的首页是默认的Fragment是home_fragment页面,我在MainActivity 中的某个方法中触发跳转到clound_fragment页面中

正确方案

想仍然保留home_fragment页面,可以使用addToBackStack()方法来将home_fragment添加到返回栈中,这样在返回时可以回到home_fragment页面,通过使用setPopUpTo(R.id.home_fragment, true)方法,您可以将home_fragment添加到返回栈中,这样在跳转到cloud_fragment后,home_fragment不会被销毁,而是会保留在返回栈中。

java 复制代码
// 在MainActivity.java中
// 在需要触发的方法中调用以下代码实现自动跳转到cloud_fragment
// 在触发跳转的方法中
private void triggerMethod() {
    // 找到NavController
    navController = Navigation.findNavController(this, R.id.fragment_container);
    // 手动导航到cloud_fragment并将home_fragment添加到返回栈
    navController.navigate(R.id.cloud_fragment, null, new NavOptions.Builder().setPopUpTo(R.id.home_fragment, true).build());
}

踩坑集合

方案1

使用传统Fragment替换方法,经测试不适用我的项目,因为跳转方法触发后,cloud_fragment覆盖在home_fragment页面上面,紧接着我使用add方法,发现add和replace效果是相同的,我不知道是什么原因导致。

java 复制代码
ConnectFragment connectFragment = new ConnectFragment();  
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();  
transaction.replace(R.id.fragment_container, connectFragment);  
transaction.commit();

方案2

经过研究,我的发现我使用的Navigation导航,发现Navigation也是可以导航的,于是有了方案2,但是经过测试,跳转是可以跳转的,但是跳转后,home_fragment页面不见了,最后通过网上查资料发现了最终的正确方案。

java 复制代码
private void triggerMethod() {
    // 找到NavController
    navController = Navigation.findNavController(this, R.id.fragment_container);
    // 手动导航到cloud_fragment
    navController.navigate(R.id.cloud_fragment);
}
相关推荐
黄林晴2 小时前
如何判断手机是否是纯血鸿蒙系统
android
火柴就是我2 小时前
flutter 之真手势冲突处理
android·flutter
法的空间3 小时前
Flutter JsonToDart 支持 JsonSchema
android·flutter·ios
循环不息优化不止3 小时前
深入解析安卓 Handle 机制
android
恋猫de小郭3 小时前
Android 将强制应用使用主题图标,你怎么看?
android·前端·flutter
jctech3 小时前
这才是2025年的插件化!ComboLite 2.0:为Compose开发者带来极致“爽”感
android·开源
用户2018792831673 小时前
为何Handler的postDelayed不适合精准定时任务?
android
叽哥3 小时前
Kotlin学习第 8 课:Kotlin 进阶特性:简化代码与提升效率
android·java·kotlin
Cui晨3 小时前
Android RecyclerView展示List<View> Adapter的数据源使用View
android
氦客3 小时前
Android Doze低电耗休眠模式 与 WorkManager
android·suspend·休眠模式·workmanager·doze·低功耗模式·state_doze