OnDestroy() called multiple times

前端 未结 2 1925
Happy的楠姐
Happy的楠姐 2021-01-26 17:35

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

相关标签:
2条回答
  • 2021-01-26 18:07

    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.

    0 讨论(0)
  • 2021-01-26 18:29

    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.

    0 讨论(0)
提交回复
热议问题