【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);
}
相关推荐
图王大胜26 分钟前
Android SystemUI组件(11)SystemUIVisibility解读
android·framework·systemui·visibility
服装学院的IT男4 小时前
【Android 13源码分析】Activity生命周期之onCreate,onStart,onResume-2
android
Arms2064 小时前
android 全面屏最底部栏沉浸式
android
服装学院的IT男5 小时前
【Android 源码分析】Activity生命周期之onStop-1
android
ChinaDragonDreamer7 小时前
Kotlin:2.0.20 的新特性
android·开发语言·kotlin
网络研究院9 小时前
Android 安卓内存安全漏洞数量大幅下降的原因
android·安全·编程·安卓·内存·漏洞·技术
凉亭下9 小时前
android navigation 用法详细使用
android
小比卡丘12 小时前
C语言进阶版第17课—自定义类型:联合和枚举
android·java·c语言
前行的小黑炭13 小时前
一篇搞定Android 实现扫码支付:如何对接海外的第三方支付;项目中的真实经验分享;如何高效对接,高效开发
android
落落落sss14 小时前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis