【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);
}
相关推荐
消失的旧时光-194315 分钟前
Android NDK 完全学习指南:从入门到精通
android
消失的旧时光-19431 小时前
Kotlin 协程实践:深入理解 SupervisorJob、CoroutineScope、Dispatcher 与取消机制
android·开发语言·kotlin
2501_915921431 小时前
iOS 26 描述文件管理与开发环境配置 多工具协作的实战指南
android·macos·ios·小程序·uni-app·cocoa·iphone
2501_915909061 小时前
iOS 抓包实战 从原理到复现、定位与真机取证全流程
android·ios·小程序·https·uni-app·iphone·webview
2501_915106321 小时前
HBuilder 上架 iOS 应用全流程指南:从云打包到开心上架(Appuploader)上传的跨平台发布实践
android·ios·小程序·https·uni-app·iphone·webview
Meteors.2 小时前
安卓进阶——Material Design库
android·安卓
佳哥的技术分享2 小时前
kotlin基于MVVM架构构建项目
android·开发语言·kotlin
折翅鵬2 小时前
Flutter兼容性问题:Could not get unknown property ‘flutter‘ for extension ‘android‘
android·flutter
2501_916007473 小时前
免费iOS加固方案指南
android·macos·ios·小程序·uni-app·cocoa·iphone
毕设源码-邱学长7 小时前
【开题答辩全过程】以 基于Android的综合社交系统为例,包含答辩的问题和答案
android