gpt4 book ai didi

android - 什么时候会调用Fragment的onDestroyView,但是不会销毁呢?

转载 作者:可可西里 更新时间:2023-11-01 18:53:25 26 4
gpt4 key购买 nike

查看 Fragment 的生命周期,我不确定这里可能发生的情况。当 Fragment 停止 Activity 时,有两种可能的方法。

  1. 调用适当的回调,销毁 View ,然后销毁 fragment
  2. 调用回调,销毁 View ,但保持 fragment 本身存活

两种选择中的哪一种是在什么情况下完成的?是什么决定了他们中的哪一个?如果一个 fragment 被添加到后台堆栈,然后被删除/替换,为什么不把它扔掉呢?为什么要保留它?

编辑:我恍然大悟,难道这取决于 fragment 是否保留?

enter image description here

最佳答案

好像全看 fragment 有没有保留。当fragment被保留,然后在onDestroyView之后onCreateView。

当 fragment 被保留(即 setRetainInstance(true))时,旋转设备时的日志如下所示:

com.example.FragmentLifecycleTestApp W/MainFragment﹕ onAttach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreate
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreateView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onActivityCreated
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStart
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onResume
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onPause
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStop
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDestroyView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDetach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onAttach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreateView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onActivityCreated
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStart
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onResume

但是不保留的时候是这样的:

com.example.FragmentLifecycleTestApp W/MainFragment﹕ onAttach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreate
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreateView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onActivityCreated
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStart
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onResume
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onPause
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStop
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDestroyView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDestroy
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onDetach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onAttach
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreate
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onCreateView
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onActivityCreated
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onStart
com.example.FragmentLifecycleTestApp W/MainFragment﹕ onResume

关于android - 什么时候会调用Fragment的onDestroyView,但是不会销毁呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23593965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com