Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 the activity to dump all data, that is get rid of all the strings and images that are in it. I figured the best way to do this was to just kill the activity.

Well, I can't seem to figure out the callback for when the Back button is pressed. So, I have been trying to use the onPause() and the onStop() callbacks for the task but both ways force close my app. Here is the code:

public void onPause() {
    this.finish();
}
public void onStop() {
    finish();
}

I've tried multiple variations of this, but none of them seemed to work. Any ideas?

like image 212
Shaun Avatar asked Jan 24 '11 04:01

Shaun


People also ask

Does Back button destroy activity?

To destroy activity on back press, use this code. //Destroys activity -> No, it does not.

How do I close an app on back press on Android?

In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so.


5 Answers

Simple Override onBackPressed Method:

    @Override     public void onBackPressed() {             super.onBackPressed();             this.finish();     } 
like image 164
Dhiral Pandya Avatar answered Oct 04 '22 00:10

Dhiral Pandya


add this to your activity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}
like image 41
ingsaurabh Avatar answered Oct 04 '22 01:10

ingsaurabh


public boolean onKeyDown(int keycode, KeyEvent event) {
    if (keycode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
    }
    return super.onKeyDown(keycode, event);
}

My app closed with above code.

like image 37
PhuongNguyen Avatar answered Oct 04 '22 01:10

PhuongNguyen


First of all, finish() doesn't destroy your process and free up the memory. It just removes the activity from the activity stack. You'd need to kill the process, which is answered in a bunch of questions (since this is being asked several times).

But the proper answer is - Don't do it. the Android OS will automatically free up memory when it needs memory. By not freeing up memory, your app will start up faster if the user gets back to it.

Please see here for a great write-up on the topic.

like image 44
EboMike Avatar answered Oct 03 '22 23:10

EboMike


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.

like image 28
Aman Alam Avatar answered Oct 04 '22 01:10

Aman Alam