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

Android BottomNavigationView最佳实现:单Activity多Fragment还是多Activity?

Hey there! Let’s break this down since you’re torn between two BottomNavigationView implementations—first off, it totally makes sense to lean towards multiple Activities if that feels more intuitive for your app’s structure, so let’s tackle that repetition issue head-on.

BottomNavigationView: Single Activity + Fragments vs Multiple Activities

First, a quick recap of both approaches

Single Activity + Fragments

  • This is the most commonly recommended pattern you’ll see online, and for good reason:
    • It keeps navigation state more consistent (like remembering scroll positions in Fragments if handled properly)
    • Cuts down on overhead from launching new Activities constantly
    • Makes sharing common UI elements (like the BottomNavigationView itself) trivial—you only set it up once in the main Activity
  • The downside? If your app has complex, independent modules, managing Fragment transactions and back stack can get messy fast.

Multiple Activities (your preferred approach)

  • The big win here is clear separation of concerns—each module lives in its own Activity, which can make code organization simpler for large apps or teams
  • But as you noticed, repeating the BottomNavigationView setup (click listeners, selected state syncing, etc.) across every Activity is a huge pain point.

Fixing the repetition in the Multiple Activities approach

You don’t have to duplicate code! Here’s how to keep your BottomNavigationView consistent without copy-pasting:

  1. Create a BaseActivity class

    • Make all your Activities that need the BottomNavigationView extend this base class
    • Put all the BottomNavigationView logic (inflating the bar, setting click listeners, handling navigation to other Activities, syncing selected item) here once.
    • Example snippet (Kotlin):
      abstract class BaseBottomBarActivity : AppCompatActivity() {
          lateinit var bottomNav: BottomNavigationView
      
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              // Inflate a base layout with BottomNav and a content placeholder
              setContentView(R.layout.activity_base_bottom_bar)
              bottomNav = findViewById(R.id.bottom_nav)
      
              // Setup click listener once for all subclasses
              bottomNav.setOnItemSelectedListener { item ->
                  when (item.itemId) {
                      R.id.nav_home -> launchActivity<HomeActivity>()
                      R.id.nav_profile -> launchActivity<ProfileActivity>()
                      R.id.nav_settings -> launchActivity<SettingsActivity>()
                      else -> false
                  }
                  true
              }
      
              // Inject the Activity-specific content into the placeholder
              val contentView = LayoutInflater.from(this).inflate(getContentViewId(), null)
              findViewById<FrameLayout>(R.id.content_container).addView(contentView)
      
              // Set the correct selected item for this specific Activity
              setSelectedBottomNavItem()
          }
      
          // Abstract methods for subclasses to implement
          abstract fun getContentViewId(): Int
          abstract fun setSelectedBottomNavItem()
      
          // Helper function to launch Activities cleanly
          private inline fun <reified T : Activity> launchActivity() {
              startActivity(Intent(this, T::class.java))
              finish() // Optional: prevents stacking Activities
          }
      }
      
    • Then your subclass Activities just need to implement the two abstract methods—super simple:
      class HomeActivity : BaseBottomBarActivity() {
          override fun getContentViewId() = R.layout.activity_home
          override fun setSelectedBottomNavItem() {
              bottomNav.selectedItemId = R.id.nav_home
          }
      }
      
  2. Use a shared layout for the BottomNavigationView

    • Create a separate layout/bottom_navigation.xml file with your BottomNavigationView definition (menu, styling, etc.)
    • Include this in your BaseActivity’s layout (or directly in Activity layouts if you skip the base class) so you don’t repeat the UI setup across files.
  3. Sync selected state properly

    • Use launchMode="singleTop" in your Activity manifest entries to reuse existing instances instead of creating new ones
    • When launching from the bottom bar, call finish() on the current Activity if you don’t want users to navigate back to it (adjust based on your app’s flow)

What’s the official take?

Google doesn’t explicitly ban either approach, but their modern navigation tools (like Jetpack Navigation) heavily favor the single Activity + Fragment pattern because it works better with deep linking, navigation graphs, and saved state handling. That said, if your app’s architecture makes multiple Activities more logical, there’s no wrong way to do it—as long as you avoid code duplication with tricks like the BaseActivity above.

内容的提问来源于stack exchange,提问作者Nicolás Vera

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:52:39