问题
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:
- onAttach
- List item
- OnCreatView
- onViewCreate then press device Back Button
- onPause
- onStop
- onDestroyView
- 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:
- onAttach
- List item
- OnCreatView
- onViewCreate then press device Back Button
- onPause
- onStop
- onDestroyView and repeat with
- 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