Is onPause guaranteed to be called when an Activity is no longer running?

前端 未结 3 855
傲寒
傲寒 2021-01-11 17:53

I create a new thread in an activity and I rely on onPause() to interrupt the thread. If my app crashes, or the Activity somehow ceases to exist, I want the thread to be kil

3条回答
  •  孤城傲影
    2021-01-11 18:10

    Yes , onPause() will be called when an activity is no longer running. Suppose an activity is closed then the sequence of events will be onPause() -> onStop() -> onDestroy(). Even if your activity slips into the background when another activity starts or when the screen switches off, onPause() is always called even if the other two methods aren't called. So even if activity ceases, onPause() will be called and your thread will be killed.

    But when your app crashes, along with your entire activity, even the thread that you have started will be taken care of by Android by finishing it all.

提交回复
热议问题