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

切换Fragment/Activity后FarmFragment中Flock RecyclerView消失求助

RecyclerView切换页面后消失,后台重启恢复的问题

问题现象

  • 首次进入FarmFragment时,Flock对应的RecyclerView显示正常
  • 切换到其他Fragment/Activity后返回,该RecyclerView消失不见
  • 应用退至后台再重新打开,RecyclerView又能正常显示
  • 已尝试删除重建Flock相关的Adapter和Model,问题仍未解决

排查与解决方法

1. 补全Fragment生命周期的刷新逻辑

Fragment切换返回时,onResume()会被触发,在这里重新加载数据并刷新Adapter,确保视图和数据重新绑定:

override fun onResume() {
    super.onResume()
    loadFlockData() // 重新获取最新数据
    flockAdapter.notifyDataSetChanged() // 强制刷新列表
}

2. 检查RecyclerView的可见性设置

有些情况下,父布局或RecyclerView本身会在页面切换时被意外设置为GONE,可以在onViewCreated()中强制设置可见性:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    flockRecyclerView = view.findViewById(R.id.flock_recycler_view)
    flockRecyclerView.visibility = View.VISIBLE // 确保视图可见
    // 其他初始化逻辑...
}

3. 避免Fragment实例丢失

如果使用replace方式切换Fragment,会销毁之前的Fragment实例,返回时会重建,可能导致数据或视图状态丢失。可以改用add并隐藏旧Fragment的方式:

// HomeActivity中切换Fragment的示例
fun switchFragment(fragment: Fragment) {
    supportFragmentManager.beginTransaction()
        .hide(currentFragment) // 隐藏当前显示的Fragment
        .add(R.id.container, fragment)
        .addToBackStack(null)
        .commit()
}

4. 确认数据加载的时机

如果数据只在onCreate()中加载一次,返回时Fragment重建可能导致数据未重新获取。把数据加载逻辑移到onViewCreated()或onResume()中:

private fun loadFlockData() {
    // 从ViewModel/Repository获取数据
    viewModel.getFlockList().observe(viewLifecycleOwner) { flockList ->
        flockAdapter.submitList(flockList)
        // 调试时打印数据大小,确认数据是否存在
        Log.d("FarmFragment", "Flock list size: ${flockList.size}")
    }
}

5. 检查Adapter的状态更新

如果使用ListAdapter,确保submitList()被正确调用,并且使用viewLifecycleOwner观察数据,避免内存泄漏或数据未更新:

// 在Fragment中观察数据
viewModel.flockList.observe(viewLifecycleOwner) { newList ->
    flockAdapter.submitList(newList)
}

相关代码示例

FarmFragment核心代码

class FarmFragment : Fragment() {
    private lateinit var flockRecyclerView: RecyclerView
    private lateinit var flockAdapter: FlockAdapter
    private val viewModel: FarmViewModel by viewModels()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_farm, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initRecyclerView()
        loadFlockData()
    }

    private fun initRecyclerView() {
        flockRecyclerView = view.findViewById(R.id.flock_recycler_view)
        flockAdapter = FlockAdapter()
        flockRecyclerView.apply {
            adapter = flockAdapter
            layoutManager = LinearLayoutManager(context)
            visibility = View.VISIBLE
        }
    }

    private fun loadFlockData() {
        viewModel.flockList.observe(viewLifecycleOwner) { flockList ->
            flockAdapter.submitList(flockList)
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.refreshFlockList() // 触发数据刷新
    }
}

HomeActivity切换Fragment代码

class HomeActivity : AppCompatActivity() {
    private lateinit var currentFragment: Fragment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)
        currentFragment = FarmFragment()
        supportFragmentManager.beginTransaction()
            .add(R.id.fragment_container, currentFragment)
            .commit()
    }

    // 切换到其他Fragment的方法
    fun goToOtherFragment() {
        val otherFragment = OtherFragment()
        supportFragmentManager.beginTransaction()
            .hide(currentFragment)
            .add(R.id.fragment_container, otherFragment)
            .addToBackStack(null)
            .commit()
        currentFragment = otherFragment
    }

    // 返回时恢复之前的Fragment
    override fun onBackPressed() {
        if (supportFragmentManager.backStackEntryCount > 0) {
            supportFragmentManager.popBackStack()
            currentFragment = supportFragmentManager.findFragmentById(R.id.fragment_container)!!
        } else {
            super.onBackPressed()
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 21:54:57