我得到了3个片段和1个活动.MainActivityCameraFragmentHomeFragmentHistoryFragment

我只想保存所有HistoryFragment个状态,这样无论何时用户访问它,它都不会再有recreated个状态.

不管怎样,我已经找到了最常见的解决方案

这是我所有的代码,

MainActivity.kt

    class MainActivity : AppCompatActivity() {
    
         private val historyFragment = HistoryFragment()
         private val homeFragment = HomeFragment()
         private val cameraFragment = CameraFragment()
         private val fragmentManager = supportFragmentManager
    
    
         override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
    
         val ft = fragmentManager.beginTransaction()
                   ft.add(R.id.fragmentContainerView, historyFragment, Constants.TAG_HISTORY)
                     .hide(historyFragment)
                     .add(R.id.fragmentContainerView, homeFragment, Constants.TAG_HOME)
    
         cameraViewModel.activeFragment = if (hasPermission()) {
                     //camera
                     ft.add(R.id.fragmentContainerView, cameraFragment, Constants.TAG_CAMERA)
                     ft.hide(homeFragment)
    
                     cameraFragment
             } else {
                     //home
                     homeFragment
                    }
         ft.commit()

         val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
         val navController = navHostFragment.findNavController()
         binding.bottomNav.setupWithNavController(navController)

         binding.bottomNav.setOnItemSelectedListener { item ->
             when (item.itemId) {
                 R.id.historyFragment -> {

                 when (cameraViewModel.activeFragment) {
                    is HomeFragment -> fragmentManager.beginTransaction().hide(cameraViewModel.activeFragment).show(historyFragment).commit()
                 }
         
                cameraViewModel.activeFragment = historyFragment
                item.isChecked = true
                true
            }
            
    }
}

HistoryFragment.kt

class HistoryFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
        ): View {

        binding = FragmentHistoryBinding.inflate(inflater, container, false)

        // tool bar
        (activity as AppCompatActivity).setSupportActionBar(binding.toolBarHistory)

        val appBarConfiguration = AppBarConfiguration(
            setOf(R.id.historyFragment, R.id.homeFragment, R.id.cameraFragment)
        )

        NavigationUI.setupActionBarWithNavController(requireActivity() as AppCompatActivity, findNavController(), appBarConfiguration)

        return binding.root
    }
}

toolbar in fragment_history.xml

<androidx.appcompat.widget.Toolbar
            android:id="@+id/tool_bar_history"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"/>

problem是我打开应用程序时,MainActivity运行,然后它显示homeFragment,当我点击bottom navigation中的history项时,homeFragment显示hide,它显示shows historyFragment,但标题栏not History中有标签Home

我已经使用了logcat,并且我检测到当我调用:navController.currentDestination?.id in historyFragment时,我得到的是homeFragmentid

我希望你能给我解决这个问题的办法,或者任何其他方法,让我save fragment岁的时候不会有虫子 先谢谢你

推荐答案

嗯,这应该是一个 comments ,但我不能. 尽管如此,我还是与你分享,以防你仍然找不到解决方案.

我没有太多关于您要从片段中存储什么内容或为什么要存储的上下文. 我仍然认为,如果您希望在活动时存储片段[数据或步骤]的整个状态,我建议使用活动范围的视图模型

private val someViewModel: SomeViewModel by activityViewModels()

因此,您希望从历史片段中存储的每个更改都可以存储在视图模型中,然后如果您转到另一个片段,然后返回到片段,它将获得该数据并将其显示给用户,因此它将使用最新的数据重新创建,而不是从头开始.

作为查找此委托的附加数据,您应该添加依赖项:

implementation "androidx.fragment:fragment-ktx:1.6.1"

Android相关问答推荐

RemoteActivityHelper.startRemoteActivity不适用于Android Wear OS 4模拟器

为什么Ionic-checkbox不总是刷新Android上的视图?

使用Android Jetpack Compose,为子Composable定义ViewModel是不是一种糟糕的做法?

为什么柱子的高度不都一样?

在androidStudio中,如何使用带有ResolutionStrategy的ResolutionSelector而不是setTargetResolve()?

在 Compose 中,当用户持续向下滚动时,LazyColumn 不会显示新项目

解决失败:Landroidx/compose/runtime/PrimitiveSnapshotStateKt

我需要在房间数据库中保留旧的自动迁移行吗?

Android:使用依赖项 ViewModelProviderFactory 初始化 ViewModel 的正确方法

从 HiltViewModel @Injection 访问 Application()

Jetpack Compose 中带有权重的行和 AnimatedVisibility 会 destruct UI

使用 Dagger Hilt 获取接口的所有实例

Jetpack Compose 动画性能问题

无法 HEAD 'https://jcenter.bintray.com/com/facebook/react/react-native/maven-metadata.xml'

Android Compose:LazyColumn 和 Column with verticalScroll 的区别

从expose 的 dropdownMenu 可组合、jetpack 组合中 Select 选项时,不会触发文本字段的 onValueChange

如何使用文件提供程序将视频从一个应用程序共享到另一个应用程序?

0dp 大小的可组合文件是否可以组合?

Jetpack 使用 Canvas 组成半圆

Android:在模块 jetified-play-services-measurement 和 jetified-play-services-measurement-impl 中发现重复类