I was playing with the activity life cycle today. I had logged all the lifecycle methods. I was on the MainActivity
and then switched off the phone suddenly. I noticed 2 methods being called:
onPause
,
onStop
,
but onDestroy
never got called.
Any take on this?
CodePudding user response:
If by "switched off the phone" you mean you turned off the screen, your results are fairly common. There will be differences between devices, due to OS version and manufacturer changes, so you should not make any assumptions. However, I would not expect your activity to be destroyed.
If by "switched off the phone" you mean you completely powered down the phone, there is no guarantee that the OS will take the time to destroy all running components. Once again, there will be differences between devices, due to OS version and manufacturer changes, so you should not make any assumptions.
CodePudding user response:
Is normal because:
onDestroy() is called before the activity is destroyed. The system invokes this callback either because:
the activity is finishing (due to the user completely dismissing the activity or due to finish() being called on the activity), or
the system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode)
https://developer.android.com/guide/components/activities/activity-lifecycle#ondestroy