Android : startActivityForResult() with BACK button functionality
Asked Answered
G

3

12

I would like to start a new activity for a result, with startActvityForResult(), but I would like to have the back button working as normal in the new activity.

Currently when I invoke a new Activity for result, nothing happens when I press the back button in the new Activity.

I tried something like this:

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

in the new Activity, but it didn't work. Still nothing happens when the back button is pressed.

Is there a way around this?

EDIT : I could of course load the last Activity in the onBackPressed() (can I?), but it seems like a rather crappy hack.

Alex Ady's answer solves my problem, but I still don't understand why onBackPressed() doesn't work. The working code now is something like this:

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

I could use an explanation.

Gantrisin answered 3/8, 2011 at 13:57 Comment(3)
Have you tried overridding the onKeyevent()Zenda
I'm trying to manipulate the hardware back button's behavior, and I'm pretty sure that the onBackPressed() method is called when the back button is pressed.Gantrisin
Sorry bro, but that did work :)Gantrisin
M
14

You could try

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
         finish();
    }
    return super.onKeyDown(keyCode, event);
}
Moorwort answered 3/8, 2011 at 14:2 Comment(5)
Although can you tell me why the onBackPressed didn't work? Strange isn't it?Gantrisin
It depends on what phone are you developing. If you are developing on the emulator, onBackPressed should work.Moorwort
Then it is strange, it should work. In fact you shouldn't override it at all.Moorwort
Yeah, I tried that too (not overriding) that didn't work either. Actually I had overriden it in the first place because it wasn't working.Gantrisin
but can anybody tell me , why onbackpressed() not work in this case ??Lacustrine
H
8

You shouldn't have to override the Back button behavior at all. By default, if the user presses the back button, the result will be Activity.RESULT_CANCELED.

Hymn answered 3/8, 2011 at 14:7 Comment(3)
Isn't working if I don't handle the event. It just stays in the current Activity.Gantrisin
Brigham is right, pressing back by default finishes the current activity. So all this nonsense with overriding event functions shouldn't be needed. Are you doing something in your activity to prevent normal operation of the back button in the first place?Raveaux
Do you have another subclass of Activity between this class and the android Activity class that is overriding onBackPressed? That might be what's causing this.Hymn
C
0

Try getting rid of the line that contains the finish().

Chipboard answered 3/8, 2011 at 14:1 Comment(2)
Tried it. Nothing changed. :(Gantrisin
Then what you could try is setting the Result to 0 ouside of this method as your default. And then if one of your conditions is met, you could change it to 1. This way, you don't have to override the onBackPressed() method.Chipboard

© 2022 - 2024 — McMap. All rights reserved.