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

关于NavigationAdvancedSample中底部导航Fragment不被detached及EditText数据留存的技术咨询

Hey there! Let's break down your questions about the NavigationAdvancedSample step by step:

Q1: Preventing Bottom Navigation Bar Fragments from being detached (and how the sample does it)

How to implement this in your own code

By default, the Navigation component replaces fragments when switching bottom navigation destinations, which detaches the previous fragment from the FragmentManager. To prevent this (so fragments are just hidden instead of detached), you'll need to override the default fragment navigation behavior to use hide()/show() instead of replace().

How NavigationAdvancedSample achieves this

The sample uses a custom FragmentNavigator implementation called KeepStateNavigator to handle this logic. Here's where the magic happens:

  1. Custom Navigator Setup: In MainActivity, they create this custom navigator and add it to the NavController's navigator provider:

    val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
    val navigator = KeepStateNavigator(this, navHostFragment.childFragmentManager, R.id.nav_host_fragment)
    navHostFragment.navController.navigatorProvider.addNavigator(navigator)
    
  2. Override Navigation Logic: The KeepStateNavigator overrides the navigate() method. Instead of replacing the current fragment, it:

    • Hides the currently displayed fragment
    • If the target fragment already exists in the FragmentManager, it shows it
    • If not, it adds the new fragment to the container

This way, the Home (Title) Fragment is never detached—just hidden—when switching to Leaderboard or Register. That's why it stays in memory instead of being detached like in your own code.

Q2: Why EditText data persists in Register Fragment even when it's detached

Great question! Even though the Register Fragment gets detached, its data doesn't disappear because of how Android handles fragment and view state:

  • Detach vs. Destroy: When a fragment is detached, its instance stays alive in memory—only its view hierarchy is destroyed. This is different from fragment destruction, where the entire instance is wiped.
  • Automatic View State Saving: Android automatically saves the state of views like EditText when the fragment's onDestroyView() is called. This state is stored in the fragment's SavedStateRegistry.
  • State Restoration: When the Register Fragment is reattached and onCreateView() is called again, the system restores the saved view state to the newly created view hierarchy. This includes the text you typed into the EditText—no ViewModel required!

This behavior is enabled by default, as long as you don't disable view state saving (e.g., by calling setSaveEnabled(false) on the EditText or fragment).


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 08:37:27