【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);
}
相关推荐
xq95272 小时前
带你玩转kakao登录 接入教程
android
2501_946233892 小时前
Flutter与OpenHarmony应用设置页面完整开发
android·flutter
青莲8434 小时前
Java基础篇——第一部
android·前端
城东米粉儿4 小时前
Android 插件 笔记
android
Jet_584 小时前
一次完整的 Unity Mono 安卓游戏逆向:Frida Hook 绕过碰撞死亡判定
android·游戏·unity
iReachers5 小时前
为什么HTML打包安卓APP安装时会覆盖或者报错?
android·java·html·html打包apk·网页打包
纟 冬5 小时前
Flutter & OpenHarmony 运动App运动模式选择组件开发
android·java·flutter
冷雨夜中漫步5 小时前
OpenAPITools使用——FAQ
android·java·缓存
ljt27249606617 小时前
Compose笔记(六十五)--插槽模式
android·笔记·android jetpack
6666v67 小时前
Android Kotlin(3) Flow异步流
android·kotlin