This might sound very basic, but in onDetach
why/what should I be cleaning up? If the fragment is being destroyed shouldn't references, such as the Context, be automatically cleaned up for me?
Asked
Active
Viewed 1,516 times
3

Daiwik Daarun
- 3,804
- 7
- 33
- 60
1 Answers
2
shouldn't references, such as the Context, be automatically cleaned up for me?
Yes.
in onDetach why/what should I be cleaning up?
Nothing. You may go through your entire Android career without ever having a good reason to override this method.

G. Blake Meike
- 6,615
- 3
- 24
- 40
-
Is [this question](http://stackoverflow.com/questions/13421945/retained-fragments-with-ui-and-memory-leaks) only referencing retained fragments then? Also, I"m guessing I should still clean up views though in onDestroyView? Thanks for the response :) – Daiwik Daarun Sep 01 '14 at 19:24
-
1Yes. If your Fragment holds a reference to its Activity (a silly idea, given the Fragment method getActivity()) it should forget it in onDetach. – G. Blake Meike Sep 01 '14 at 19:28
-
2I am not sure that this answer is entirely correct. For Fragments, it is possible that onDestroy() is not called. Such is the case when a Fragment is detached, only onPause, onStop and onDestroyView methods are called. In the event that a Fragment is removed, however, its onPause, onStop, onDestroyView, onDestroy and onDetach methods are called. With that being said, I onDestroyView is suggested to add your closing logic to. – portfoliobuilder Jan 25 '16 at 20:05