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

139,568

Solution 1

add this to your activity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}

Solution 2

Simple Override onBackPressed Method:

    @Override
    public void onBackPressed() {
            super.onBackPressed();
            this.finish();
    }

Solution 3

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.

Solution 4

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.

Solution 5

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.

Share:
139,568
Shaun
Author by

Shaun

Updated on July 09, 2022

Comments

  • Shaun
    Shaun almost 2 years

    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?

  • EboMike
    EboMike over 13 years
    What's the point? Pressing the back button will automatically go down the activity stack. finish() just makes sure that you can't go back. (That said, the question is admittedly vague.)
  • ingsaurabh
    ingsaurabh over 13 years
    of course but the activity is killed is not certain on press of back button so to kill finish is important and thats what asked in question but since releasing up memory goes its done automatically by OS
  • EboMike
    EboMike over 13 years
    Absolutely not. If the activity is not in front, it can be reclaimed by the OS. Finished or not.
  • EboMike
    EboMike over 13 years
    Besides, I see this hack with KEYCODE_BACK a lot around here. What if the user presses the HOME button? What if the user holds the HOME button to pick a different app? What if the app switched because of a phone call? OR a notification? In most cases, if you're checking for KEYCODE_BACK, you're doing it wrong.
  • ingsaurabh
    ingsaurabh over 13 years
    Dont know what you are saying but but its not necessary to be reclaim so in order to properly dispose this hack is used and what you are saying is not asked in question I only answered that thing which is asked and this is the way most android app are using any other way then plz suggest
  • Shaun
    Shaun over 13 years
    I see where everyone is coming from, but this is the answer I was looking for. If they wanted to go back to the activity, I would want them to reload it anyways as the information could have been changed on the server. Thank you for your help on this.
  • Shaun
    Shaun over 13 years
    If the activity is using alot of resources, then why not kill it from inside of your application as opposed to wait for the GC? Thats a whole nother process that would have to run through, hence slowing the phone down. Good memory management is making every attempt to manage it within the program itself. Thats why most managed programming languages and ide's automatically put in self cleanup code into the program, and its also the reason why you are giving the life cycle, so that you may manage memory the best you can from within your applicatoin.
  • Fallenreaper
    Fallenreaper over 11 years
    i was looking at this, and it seems that i will call finish, but memory isnt released. SO if i start the activity, then stop it, then start it again, i will get a OutOfMemoryError.. Granted, what i did was put finish in onStop instead of this set up. Let me try this way. Update: This still doesnt work.
  • dns
    dns over 10 years
    Shaun is right, good memory management is making every attempt to manage it within the program itself.
  • A.J.
    A.J. over 9 years
    Answer by EboMike below is the correct answer. Finishing the activity does NOT necessarily release memory.
  • Keshav Gera
    Keshav Gera over 6 years
    Its Working but app is open same page always whenever app is not killed
  • Keshav Gera
    Keshav Gera over 6 years
    but want to start app in Launcher actiivity