37

Imagine this scenario: I have a Fragment in a Pager. I try to switch to other apps, so that the Activity owning my pager (and my fragment) will be stopped and temporarily destroyed, eventually.

So, when I come back to my Activity, the Fragment's callbacks onCreate, oncreateview and so forth are called. But none of the Fragment's onDestroy callbacks were called before! It seems that after "onStop" the fragment is destroyed at once. Is it a normal behavior? Is it maybe because the Activity is destroyed without a call to its onDestroy?

Bertuz
  • 2,390
  • 3
  • 25
  • 50
  • I would need a call to a `onDestroy` or `onDestroyView` on the `Fragment` because I have a nested fragment in it that I would like to remove in order to be recreated when `onCreateView` is called back – Bertuz Jun 19 '13 at 15:49
  • Related post - [Activity's onDestroy / Fragment's onDestroyView set Null practices](https://stackoverflow.com/q/26369905/465053) & [Why implement onDestroy() if it is not guaranteed to be called?](https://stackoverflow.com/q/6117341/465053) – RBT Aug 16 '18 at 06:31

1 Answers1

45

Take a look at this question: Why implement onDestroy() if it is not guaranteed to be called?

Basically, onDestroy() is only guaranteed to be called if you call finish(). Otherwise, onDestroy() may not be called until the system deems it necessary. You might want to look at putting your "closing" logic in the onPause() or onStop() instead.

Community
  • 1
  • 1
invertigo
  • 6,336
  • 5
  • 39
  • 64
  • 5
    Thanks for the link! I was already aware of it, but I was still wondering if the `Fragment`'s `onDestroy` wasn't called because of that. So, you're confirming that the `Fragment`'s `onDestroy` is not called because its `Activity`'s `onDestroy` is not called either. Did I get right? – Bertuz Jun 19 '13 at 16:57