Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android : startActivityForResult() with BACK button functionality

Tags:

People also ask

Is startActivityForResult deprecated?

onActivityResult , startActivityForResult , requestPermissions , and onRequestPermissionsResult are deprecated on androidx.

What can I use instead of startActivityForResult?

But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.

Which of the following are valid variants of startActivityForResult () method?

Method Signature There are two variants of startActivityForResult() method. Video Player is loading. This is a modal window. The media could not be loaded, either because the server or network failed or because the format is not supported.


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.