onDetach不需要片段吗? [英] onDetach not called for fragment?
问题描述
我编写了将Activity A启动到Activity B的代码.ActivityA和B都有片段实现.
I write an code to launch Activity A to Activity B. Both Activity A and B has fragment implementation.
方案:如果活动A频繁启动包含片段的活动B,那么大多数情况下它会错过Fragment.onDetach ..我用log进行检查,通常它会给我以下重写方法日志:
Scenario: If Activity A frequently launch Activity B which contain Fragment, then most of times it missed Fragment.onDetach..I checked with log, normally it give me following override method log:
- onAttach
- 列表项
- OnCreatView
- onViewCreate,然后按设备的后退"按钮
- onPause
- onStop
- onDestroyView
- onDetach
现在我按下活动B中的设备后退"按钮,它再次启动活动A,然后启动活动B并频繁重复相同的序列,然后按以下顺序更改日志序列:
now I press device Back button from Activity B which again launch Activity A then it launch Activity B and repeat same sequence frequently, then log sequence get change in following order:
- onAttach
- 列表项
- OnCreatView
- onViewCreate,然后按设备的后退"按钮
- onPause
- onStop
- onDestroyView并重复
- 没有onDetach的onAttach
有时它在onPause之后也会重复相同的行为.
some times it repeat same behaviour after onPause also.
我正在使用
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, fragment).addToBackStack(null).commitAllowingStateLoss(); to add fragment in Activity.
有什么我想念的吗..有什么建议吗?
Is there anything I am missing..any suggestion ?
推荐答案
片段被销毁后将被分离.销毁视图后,您直接完成的操作会立即取消. 请记住,销毁视图和销毁视图是片段中的两个不同的事物. 因此,请尝试onDestroyView,然后依次选择onDestroy和onDetach.
A fragment is detached after it is destroyed. what u have done is dettached directly after destroying view. Remember destroying and destroying view are two different things in fragment. So try onDestroyView, then onDestroy and then onDetach.
这篇关于onDetach不需要片段吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!