Setresult Does Not Work When Back Button Pressed

setResult does not work when BACK button pressed

I refactored my code. Initially I prepared some data and set it as activity result in onDestroy (this did not work). Now I set activity data each time the data to be returned is updated, and have nothing in onDestroy.

Returning intent result when Activity is closed by back button

Since this question is still getting attention, i am posting a more correct answer than the one i accepted two years ago, thanks for MasterGaurav for the tip.

@Override
public void onBackPressed() {
Intent intent = new Intent();
intent.putIntegerArrayListExtra(SELECTION_LIST, selected);
setResult(RESULT_OK, intent);

super.onBackPressed();
}

onActivityResult not called when pressed back arrow on screen but only called when back button pressed in android?

Yes it is not called because Up button already having the information of Parent Activity statically. So when you press it should go only that Parent Activity.

For example You have three Activities like A, B, C.

    B parent Activity is C

<activity android:name=".SecondActivity" android:parentActivityName=".ThirdActivity"/>

programmatically you go From A --> B

Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivityForResult(intent,1);

Now you press the Up button(back arrow) of (Second) Activity B then it should not go to any Activity. Because it is confused. Regarding manifest file parent Activity is ThirdActivity C but regarding Stack parent Activity is A.

  1. There is some differences between the Up and Back button.

  2. The Up button is used to navigate within an app based on the hierarchical relationships between screens.

  3. When you press Up button it creates the "New Instance" of the Parent Activity (That is only reason OnActivityResult() method not called here).

  4. But the Back button used to navigate, in reverse chronological order, through the history of screens the user has recently worked with.(the Previous Instance used here, instead of creating new Instance. That is only the reason OnActivityResult() method called in the MainActivity (Parent)).

  5. The Up button, which ensures the user remains within your app, the Back button can return the user to the Home screen, or even to a different app (because, it navigates based on history).

  6. If you want same behavior of Back button for Up button. you should write like this.

      @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
    case android.R.id.home:
    super.onBackPressed();
    break;
    }
    return true;
    }

onActivityResult() not triggered on onBackPressed()

As per documentation in android for startActivityForResult

Same as calling startActivityForResult(Intent, int, Bundle) with no
options.

intent Intent: The intent to start.

requestCode int: If >= 0, this code will be returned in
onActivityResult() when the activity exits.

Now what you did is this: startActivityForResult(intent, Activity.RESULT_OK);

Where Activity.RESULT_OKactual value is -1 that is why it is not returning the result.
You need to have a valid request code when you start an activity for result and then filter the request code in your onActivityResult.

onActivityResult not working with onBackPressed?

You need to return the result when you press the ActionBar back/home button, but you added the code in the bottom back button.

So, transfer the code in Activity2 to be handled when the ActionBar back button is pressed, so Override onOptionsItemSelected, and the home button has an id of android.R.id.home.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent intent = new Intent(this, Activity1.class);
intent.putExtra("MESSAGE", "Hello from Activity 2!");
Log.i("TEST", "Setting result...");
setResult(RESULT_OK, intent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

activity not sending result when pressing back button

To pass data from child activity to parent activity use the following code:

Intent intent= childActivity.getIntent();
intent.putExtra("key","value");
childActivity.setResult(Activity.RESULT_OK, intent);
childActivity.finish();

if value is an list of object use Serializable .

Now if you press back button , onActivityResult method of parent class will be called and you will get RESULT_CANCEL.

onActivityResult is not called when the back button in ActionBar is clicked

Here is the code which is working:

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case android.R.id.home:
// back button
Intent resultIntent = new Intent();
setResult(Activity.RESULT_OK, resultIntent);
finish();
return true;

}
return super.onOptionsItemSelected(item);
}

I guess the finish() will close the current Activity, and return true inform that action has been processed. (The default back action seems to be different from finish().)

App crashed when the return button is clicked

Ok, shamelessly copy-pasting from the official docs at http://developer.android.com/training/basics/intents/result.html:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.

// Do something with the contact here (bigger example below)
}
}
}

Make sure you have the if (resultCode == RESULT_OK) line in your code. If (resultCode == RESULT_CANCELED), then no result will be available and the Intent is null.

When you press the "back" button on your device the normal result is the cancellation of the current activity, hence the calling activity gets the resultCode of RESULT_CANCELED in that case and not the one you set when explicitly returning via finish().



Related Topics



Leave a reply



Submit