How do I kill an Activity when the Back button is pressed?

前端 未结 5 483
傲寒
傲寒 2020-11-30 04:59

I got an Activity that when it starts, it loads an image from the internet. In an effort to save memory, when the Activity is left by the back button being pressed, I want t

5条回答
  •  [愿得一人]
    2020-11-30 05:26

    Well, if you study the structure of how the application life-cycle works,here , then you'll come to know that onPause() is called when another activity gains focus, and onStop() is called when the activity is no longer visible.

    From what I have learned yet, you can call finish() only from the activity which is active and/or has the focus. If you're calling finish() from the onPause() method that means you're calling it when the activity is no longer active. thus an exception is thrown.

    When you're calling finish() from onStop() then the activity is being sent to background, thus will no longer be visible, then this exception.

    When you press the back button, onStop() is called.

    Most probably, Android will automatically do for you what you are currently wanting to do.

提交回复
热议问题