You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android:BottomNavigationView跳转Activity后导航栏消失问题求助

这个问题其实是很多刚用BottomNavigationView的开发者都会踩的坑——你现在是直接启动全新的Activity,每个Activity都是独立的页面容器,自然不会带上原来MainActivity里的底部导航栏,所以跳转后导航栏就消失了。下面给你两种解决方案,推荐优先用第一种:

方案一:单Activity + 多Fragment(官方推荐)

这是Android官方针对BottomNavigationView推荐的使用方式,页面切换更流畅,也更符合现代Android开发的最佳实践。

步骤1:修改MainActivity的布局

在原来的布局里添加一个FrameLayout作为Fragment的容器,让底部导航栏固定在页面底部:

<!-- activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Fragment容器:用来加载不同页面的内容 -->
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <!-- 底部导航栏 -->
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_nav_menu" />
</LinearLayout>

步骤2:将原Activity转为Fragment

把你原来的SetData Activity改成Fragment,比如创建SettingFragment,把set_data.xml的内容直接作为Fragment的布局:

// SettingFragment.java
public class SettingFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 加载原set_data.xml布局
        return inflater.inflate(R.layout.set_data, container, false);
    }
}

同理,把其他需要跳转的页面也转为Fragment(比如首页对应的HomeFragment)。

步骤3:在MainActivity中处理导航切换

给BottomNavigationView设置点击事件,切换对应的Fragment:

// MainActivity.java
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
        bottomNav.setOnItemSelectedListener(item -> {
            Fragment selectedFragment = null;
            int itemId = item.getItemId();
            
            // 根据点击的菜单项选择对应的Fragment
            if (itemId == R.id.nav_home) {
                selectedFragment = new HomeFragment();
            } else if (itemId == R.id.nav_settings) {
                selectedFragment = new SettingFragment();
            }
            // 其他导航项可以继续添加
            
            // 替换Fragment容器里的内容
            if (selectedFragment != null) {
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.fragment_container, selectedFragment)
                        .commit();
            }
            return true;
        });

        // 首次启动默认显示首页Fragment
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, new HomeFragment())
                    .commit();
        }
    }
}
方案二:多Activity共享底部导航栏(兼容原有代码)

如果你想保留原来的多Activity结构,可以把底部导航栏抽成公共布局,让每个Activity都引入它:

步骤1:创建公共底部导航栏布局

新建一个layout_bottom_nav.xml,把BottomNavigationView的代码单独放这里:

<!-- layout_bottom_nav.xml -->
<com.google.android.material.bottomnavigation.BottomNavigationView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:menu="@menu/bottom_nav_menu" />

步骤2:在每个Activity的布局中引入公共导航栏

比如修改set_data.xml,在页面底部include这个公共布局:

<!-- set_data.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- 原来的设置页面内容,用layout_weight让它占满除导航栏外的空间 -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <!-- 你的设置页面内容 -->
    </ScrollView>

    <!-- 引入公共底部导航栏 -->
    <include layout="@layout/layout_bottom_nav" />
</LinearLayout>

其他需要显示导航栏的Activity布局也要做同样的修改。

步骤3:在每个Activity中处理导航逻辑

比如在SetData.java中,设置当前选中的菜单项,并处理跳转逻辑:

// SetData.java
public class SetData extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_data);

        BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
        // 设置当前页面对应的菜单项为选中状态
        bottomNav.setSelectedItemId(R.id.nav_settings);
        
        bottomNav.setOnItemSelectedListener(item -> {
            Intent intent;
            int itemId = item.getItemId();
            
            if (itemId == R.id.nav_home) {
                intent = new Intent(this, MainActivity.class);
            } else if (itemId == R.id.nav_settings) {
                // 已经在当前页面,无需跳转
                return true;
            } else {
                // 其他导航项的跳转逻辑
                intent = new Intent(this, OtherActivity.class);
            }
            
            // 避免重复创建Activity实例,提升跳转效率
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
            finish(); // 可选:关闭当前Activity,减少栈内页面数量
            return true;
        });
    }
}
两种方案对比
  • 单Activity+Fragment:页面切换无延迟,状态维护更简单,是官方推荐的架构,适合长期维护的项目。
  • 多Activity共享导航栏:可以兼容你现有的代码结构,但页面切换有Activity跳转的开销,且每个Activity都要重复写导航逻辑,维护成本较高。

内容的提问来源于stack exchange,提问作者wowo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:35:33