Search code examples
androidonactivityresult

onActivityResult not getting called in an activity


I know this has been asked here and here. But I am doing all that is mentioned in these questions but still my onActivityResult() method is not getting called.

What am I doing wrong here?

caller activity

private void launchAddressActivity() {
    Intent intent = new Intent(PaymentActivity.this, AddressActivity.class);
    startActivityForResult(intent, 2);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult: called");
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == 2 && data != null) {
        if (validIntent(data)) {
            addressTextView.setText(
                    MessageFormat.format("{0},{1}\n{2}",
                            data.getStringExtra(UserConstants.USER_ADDRESS_1),
                            data.getStringExtra(UserConstants.USER_ADDRESS_2),
                            data.getStringExtra(UserConstants.USER_ADDRESS_LANDMARK)));
       }
    }
}

callee activity

Intent resultIntent = new Intent();
resultIntent.putExtra(UserConstants.USER_ADDRESS_1, address1.getText().toString());
resultIntent.putExtra(UserConstants.USER_ADDRESS_2, address2.getText().toString());
resultIntent.putExtra(UserConstants.USER_ADDRESS_LANDMARK, landmark.getText().toString());
setResult(2, resultIntent);

manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vuclip.liqyd">

<application
    android:name=".LiqydApp"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:noHistory="true"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".ui.login.LoginRegistrationActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <!--<activity android:name=".ui.login.LoginRegistrationActivity" />-->
    <activity android:name=".ui.gallary.GallaryActivity" />
    <activity android:name=".ui.address.AddressActivity" />
    <activity android:name=".ui.order_details.OrderDetailsActivity" />
    <activity android:name=".ui.payment.PaymentActivity" />
    <activity android:name=".ui.MenuHandingActivity" />
</application>


Solution

  • Change

    setResult(2, resultIntent);
    

    to

    setResult(Activity.RESULT_OK, resultIntent);
    

    And,

    if (resultCode == 2 && data != null) {
    

    to

    if (requestCode == 2 && data != null) {
    

    That would be the right way to use startActivityForResult.

    UPD: as we found in chat the reason to fail here after fixing what described above was mistakenly using RESULT_OK as tracking code here startActivityForResult(intent, RESULT_OK);, the onActivityResult callback was not called in this case. After changing it to startActivityForResult(intent, 2); everything finally started working.