Search code examples
androidandroid-intentandroid-activityintentfilter

Every time onActivityResult returning data as null in multi chain activity


First of all, every answer of the related questions did not solve my problem, that's why I am creating a new question.

I have two apps. From the first app, I am calling intent for ACTION_VIEW with a URI with onActivityResult.

In the second app, I have set an intent filter for the same URI.

Now from Activity B, I am calling Activity C in App2 with onActivityResult.

From Activity C, I am calling Activity D with onActivityResult.

After some work in Activity D will set a result with some data in the bundle and set the result as OK.

Instead of getting this data in Activity C, I am getting intent in Activity B with data as null in the bundle.

And Activity B is sending the same response to Activity A of App1. Hence I am getting null data in App1 as well.

In Request,

App1 (Activity A) ---> App2 (Activity B) ---> Activity C ---> Activity D

In Response,

Expected : App1 (Activity A) <--- App2 (Activity B) <--- Activity C <--- Activity D

Actual : App1 (Activity A) <--- App2 (Activity B) <--- Activity D
with data as null.

Kindly help me to get data from Activity D of App2 to Activity A of App1.

CODE:

App1 Activity A:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(deepLinkUrl));
Intent chooser = Intent.createChooser(intent, title);
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivityForResult(chooser, 1);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (data.getExtras() != null) {
            Log.e("onActivityResult", "responseIntent.getExtras() " + 
                     data.getExtras());
        }
    }
}

App2 Activity B:

Constants.deeplinkUri = getIntent().getData();
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
intent.setData(Uri.parse(Constants.deeplinkUri));
startActivityForResult(intent, 100);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100) {
        if (data.getExtras() != null) {
            Log.e("onActivityResult", "responseIntent.getExtras() " + 
                     data.getExtras());
        }
        setResult(Activity.RESULT_OK, data);
        finish();
    }
}

App2 Activity C:

Intent intent = new Intent(LoginActivity.this, PayActivity.class);
intent.setData(Uri.parse(Constants.deeplinkUri));
startActivityForResult(intent, 100);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 100) {
        if (data.getExtras() != null) {
            Log.e("onActivityResult", "responseIntent.getExtras() " + 
                     data.getExtras());
        }
        setResult(Activity.RESULT_OK, data);
        finish();
    }
}

App2 Activity D:

Intent returnIntent = new Intent();
returnIntent.putExtra("result", msg);
setResult(Activity.RESULT_OK, returnIntent);
finish();

Thanks in advance. Anyone?


Solution

  • You've a problem in calling the startActivityForResult.

    From your ActivityA you're launching ActivityB like this.

    startActivityForResult(chooser, 1); // The request code is 1
    

    From your ActivityB you're launching ActivityC with this.

    startActivityForResult(intent, 100); // Request code 100
    

    And from ActivityC you're launching ActivityD with the same request code which is 100.

    startActivityForResult(intent, 100);  // Same request code 100
    

    So when your ActivityD is setting some results and passes the result intent to the calling Activity it immediately passes the result with super call to its parent. In your case, it immediately passes the result to ActivityB and there's a handler in ActivityB to handle the request code 100 too which gets the null value.

    So you might try setting different request code from each of your Activity here to avoid this situation.

    From your ActivityB launch ActivityC like this.

    startActivityForResult(intent, 100);  // Request code 100
    

    And from ActivityC launch ActivityD like this

    startActivityForResult(intent, 101);  // Request code 101
    

    Now add the handler in ActivityC to handle the result with request code 101 in ActivityC.

    Hope that will solve your problem.