onDetach not called for fragment?

梦想与她 提交于 2019-12-11 03:21:33

问题


I write an code to launch Activity A to Activity B. Both Activity A and B has fragment implementation.

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:

  1. onAttach
  2. List item
  3. OnCreatView
  4. onViewCreate then press device Back Button
  5. onPause
  6. onStop
  7. onDestroyView
  8. onDetach

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:

  1. onAttach
  2. List item
  3. OnCreatView
  4. onViewCreate then press device Back Button
  5. onPause
  6. onStop
  7. onDestroyView and repeat with
  8. onAttach without onDetach

some times it repeat same behaviour after onPause also.

I am using

getSupportFragmentManager().beginTransaction()
        .replace(R.id.fragment_container, fragment).addToBackStack(null).commitAllowingStateLoss(); to add fragment in Activity.

Is there anything I am missing..any suggestion ?


回答1:


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.



来源:https://stackoverflow.com/questions/26421059/ondetach-not-called-for-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!