2

I simply want to exchange object information from Activity2 back to Activity1, when "back" button is pressed. Everything works fine, except back in Activity1 onActivityResult(..) is never triggered. Therefore I can't access the intent information. Anyone knows why?

I checked out these answers, but they don't work:

setResult does not work when BACK button pressed

How to pass data from 2nd activity to 1st activity when pressed back? - android

How to pass the values from one activity to previous activity

Send data to a parent activity onBackPressed

My code (if you need more, tell me):

AndroidManifest.xml:

        <activity
            android:name="com.example.Activity1"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="com.example.Activity2"
            android:theme="@style/AppTheme.NoActionBar"
            android:screenOrientation="portrait">
        </activity>

Activity1.java:

public class MainActivity extends AppCompatActivity {

    // other stuff...

    private void showActivity2(Test test) {
        Intent intent = new Intent(this, Activity2.class);
        intent.putExtra("test", test.getSerializableObject());
        startActivityForResult(intent, Activity.RESULT_OK);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_CANCELED) {
            Test test = data.getExtras().getParcelable("test");
            // Do something...
        }
    }

Activity2.java:

public class Activity2 extends AppCompatActivity {

    // other stuff....

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("test", test.getSerializableObject());
        setResult(Activity.RESULT_CANCELED, intent);
        super.onBackPressed();
    }
}
Community
  • 1
  • 1
Javatar
  • 2,518
  • 1
  • 31
  • 43
  • `onBackPressed()` most likely is too late. Call `setResult()` when the user does something positive in your UI to say what it is that they want (e.g., clicks the list item). Usually, you then also call `finish()`, as once the user decided what they want, `Activity2` is no longer needed. – CommonsWare Jan 30 '17 at 16:43
  • I also tried to setResult(xxx) while Activity2 is updating something. Still... looks like Activity1 is generally ignoring the callback. – Javatar Jan 30 '17 at 16:56

2 Answers2

6

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.

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • Thats correct. Funny, that I already used it correctly in another context... `startActivityForResult(Intent.createChooser(intent, "Select Photo"), REQUEST_GALLERY);` It is now working correctly! Thx. – Javatar Jan 30 '17 at 17:04
3

You haven't mentioned the request code on Activity1.java instead you have written Activity.RESULT_OK which is a result code.

startActivityForResult(intent, Activity.RESULT_OK);

It should be :

startActivityForResult(intent, REQUEST_CODE);

REQUEST_CODE should be a final static int value.

Farmaan Elahi
  • 1,620
  • 16
  • 18