1

I have noticed that the onDestroy() method of a fragment gets called multiple times - why would this be? I would expect only 1 call.

RunLoop
  • 20,288
  • 21
  • 96
  • 151

2 Answers2

1

onDestroy() = The final call you receive before your activity is destroyed.

This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space.

You can distinguish between these two scenarios with the isFinishing() method.

Basbous
  • 3,927
  • 4
  • 34
  • 62
-1

It's normal for an Activity or Fragment to get onDestroy()-ed multiple times. For example, when you change the device orientation, the current Activity goes through onDestroy() and then a new instance of the same Activity goes through onCreate(), now in the new orientation.

You might be confusing this for finish(), which gets called when the Activity gets "killed" per se and happens only once when you navigate away from it.

npace
  • 4,218
  • 1
  • 25
  • 35
  • 4
    This is nonsense. A **given `Activity` instance** will have `onDestroy()` called only once (or not at all). It will **never** be called more than once! If you change your device orientation, Android destroys the current active instance of the `Activity` (and calls `onDestroy()`) and then it creates **a new instance** of the `Activity`. This answer is unfortunately wrong and misleading. – David Wasser May 28 '17 at 18:22
  • 1
    In any case, the OP asked about `onDestroy()` of `Fragment`, not `Activity. – David Wasser May 28 '17 at 18:24
  • You're absolutely right, edited. Either way, the answer has been accepted, so I guess the question was asked due to the OP thinking that `onDestroy()` gets called only a single time, which is false for both `Activity` and `Fragment` – npace May 29 '17 at 08:47
  • Also reworded the answer - 'instance' sounds like I'm talking about a Java Object instance, where I actually mean 'an Activity', which can go through many `Activity` object instances during its lifecycle. – npace May 29 '17 at 08:51
  • Thanks for fixing it. However, your answer is still wrong/misleading/confusing. You write that `onDestroy()` can be called multiple times. This is wrong. That's like saying `onCreate()` can be called multiple times. Also wrong. Not only that, but in the second part of your answer you write that `finish()` is only called once. If you mean "per class and not per instance then this is also wrong (and conflicts with the statement about `onDestroy()`. Saying that `onDestroy()` can be called multiple times, even as you have written it, is misleading. Using the same logic you can say that ... – David Wasser May 29 '17 at 08:59
  • ...all methods can be called multiple times, since I can start the app and then stop it and then start it again and then all methods could be called multiple times. This is just wrong. Sorry. `onDestroy()` is called at most once. Period. Anything else is just confusing. – David Wasser May 29 '17 at 09:00
  • Once per `Activity` object. Multiple times per screen lifecycle, if you go through configuration changes and let the OS handle those for you. Either way, your input is valuable, but I'm afraid changing the answer more than I have already will turn this answer into a mess. – npace May 29 '17 at 15:12
  • You could just delete this answer. – David Wasser May 29 '17 at 15:13