【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);
}
相关推荐
问道飞鱼16 分钟前
【移动端知识】移动端多 WebView 互访方案:Android、iOS 与鸿蒙实现
android·ios·harmonyos·多webview互访
aningxiaoxixi1 小时前
Android 之 audiotrack
android
枷锁—sha1 小时前
【DVWA系列】——CSRF——Medium详细教程
android·服务器·前端·web安全·网络安全·csrf
Cao_Shixin攻城狮5 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
呼啦啦呼啦啦啦啦啦啦8 小时前
利用pdfjs实现的pdf预览简单demo(包含翻页功能)
android·javascript·pdf
idjl9 小时前
Mysql测试题
android·adb
游戏开发爱好者812 小时前
iOS App 电池消耗管理与优化 提升用户体验的完整指南
android·ios·小程序·https·uni-app·iphone·webview
人生游戏牛马NPC1号12 小时前
学习 Flutter (四):玩安卓项目实战 - 中
android·学习·flutter
星辰也为你祝福h14 小时前
Android原生Dialog
android
梁同学与Android14 小时前
Android ---【CPU优化】需要优化的原因及优化的地方
android