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

5

38

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?

Loathly answered 24/1, 2011 at 4:50 Comment(1)
what are you exactly looking for?to finish current activity on back ?buttonRingdove
M
44

add this to your activity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}
Meadow answered 24/1, 2011 at 5:6 Comment(8)
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.)Enos
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 OSMeadow
Absolutely not. If the activity is not in front, it can be reclaimed by the OS. Finished or not.Enos
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.Enos
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 suggestMeadow
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.Loathly
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.Emf
Answer by EboMike below is the correct answer. Finishing the activity does NOT necessarily release memory.Asepsis
B
82

Simple Override onBackPressed Method:

    @Override
    public void onBackPressed() {
            super.onBackPressed();
            this.finish();
    }
Berdichev answered 29/2, 2012 at 15:15 Comment(0)
M
44

add this to your activity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if ((keyCode == KeyEvent.KEYCODE_BACK))
    {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}
Meadow answered 24/1, 2011 at 5:6 Comment(8)
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.)Enos
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 OSMeadow
Absolutely not. If the activity is not in front, it can be reclaimed by the OS. Finished or not.Enos
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.Enos
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 suggestMeadow
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.Loathly
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.Emf
Answer by EboMike below is the correct answer. Finishing the activity does NOT necessarily release memory.Asepsis
C
14
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.

Chrystel answered 8/5, 2012 at 3:35 Comment(2)
Its Working but app is open same page always whenever app is not killedManicure
but want to start app in Launcher actiivityManicure
E
8

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.

Enos answered 24/1, 2011 at 5:12 Comment(2)
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.Loathly
Shaun is right, good memory management is making every attempt to manage it within the program itself.Fields
M
7

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.

Mentality answered 24/1, 2011 at 5:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.