Search code examples
androidfragmentandroid-ondestroy

Objects in Fragment get destroyed when Fragment is destroyed?


Are objects in Fragment get destroyed when Fragment it is in gets destroyed? I.e. when onDestroy() is called.


Solution

  • Having onDestroy() called on your Fragment doesn’t mean that it will be cleared from memory right away. More than that if you keep a reference to it then it won’t be garbage collected at all. But when there’s no references left at some point in time the fragment will be removed (no guarantees when exactly that happens - but usually it’s indeed soon after onDestroy()).

    All the objects in your fragment will be garbage collected after the fragment is garbage collected if you have no other references to them.