1

I have a hierarchy like this in my android app.

  • ProfileActivity

    • SettingTabFragment

      • ProfileContentFragment

From inside ProfileContentFragment I call another activity called ProfileVideoRecordingActivity using startActivityForResult()

Here is the code in ProfileContentFragment

Intent videoRecordIntent = new Intent(mActivity, ProfileVideoRecordingActivity.class);
                            startActivityForResult(videoRecordIntent, REQUEST_CODE_PROFILE_VIDEO_PATH);

Here is how I return from ProfileVideoRecordingActivity

Intent intent = new Intent();
intent.putExtra(VIDEO_PROFILE_PATH, renamedVideoFile.getAbsolutePath());
                setResult(Activity.RESULT_OK, intent);

//Go back to calling Activity
finish();

Problem is neither ProfileActivity's nor ProfileContentFragment's onActivityResult() is called.

ProfileActivity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
}

SettingTabFragment

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
}

ProfileContentFragment

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
 super.onActivityResult(requestCode, resultCode, data);
 if(requestCode == REQUEST_CODE_PROFILE_VIDEO_PATH){
        if(resultCode == Activity.RESULT_OK) {
            String profileVideoPath = data.getExtras().getString(ProfileVideoRecordingActivity.VIDEO_PROFILE_PATH);
            Log.d("DEBUG", profileVideoPath);
        }
    }
}

I tried solutions from stackoverflow where I need to call fragment's onActivityResult explicitly from activity but that didn't work. Here is what I tried.

ProfileActivity inside onActivityResult()

List<Fragment> fragments = getSupportFragmentManager().getFragments();
    if (fragments != null) {
        for (Fragment fragment : fragments) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

SettingTabFragment inside onActivityResult()

List<Fragment> fragments = getChildFragmentManager().getFragments();
    if (fragments != null) {
        for (Fragment fragment : fragments) {
            fragment.onActivityResult(requestCode, resultCode, data);
        }
    }

Both of them didn't work. What can I do?

Chandan Pednekar
  • 525
  • 5
  • 19
  • What is the `launchMode` for `ProfileVideoRecordingActivity`? – ADM May 10 '18 at 08:15
  • launchMode attribute isn't there in manifest for ProfileVideoRecordingActivity. – Chandan Pednekar May 10 '18 at 08:19
  • What is the numerical value of `REQUEST_CODE_PROFILE_VIDEO_PATH`? – Mike M. May 10 '18 at 08:21
  • @MikeM. REQUEST_CODE_PROFILE_VIDEO_PATH = 9573; I used an arbitrary number – Chandan Pednekar May 10 '18 at 08:22
  • look at : https://stackoverflow.com/questions/13580075/onactivityresult-not-called-in-new-nested-fragment-api – Mohd Saquib May 10 '18 at 08:31
  • Put logs before `if(requestCode == REQUEST_CODE_PROFILE_VIDEO_PATH)` in `onActivityResult` and check if its called. In `Activity` and `SettingTabFragment` too. – ADM May 10 '18 at 08:33
  • @ADM I already have logs in place. None of the methods are being called. – Chandan Pednekar May 10 '18 at 08:39
  • @MohdSaquib Tried both getParentFragment().startActivityForResult(intent, requestCode); and getActivity().startActivityForResult(intent, requestCode); Sadly none worked. – Chandan Pednekar May 10 '18 at 08:40
  • I case of nested Fragment the parent Fragment `onActivityResult` should get called . Take a look at link provided by Mohd Saquib. Also let us know where are you calling `setResult()` update your code in question . – ADM May 10 '18 at 08:40
  • @ADM That was the first solution I tried. In my case even the ProfileActivity's onActivityResult() is not being called. – Chandan Pednekar May 10 '18 at 08:42
  • Try to put a `super.onActivityResult` call first inside `ProfileContentFragment. onActivityResult` to see if it works – Tam Huynh May 10 '18 at 08:45
  • @TamHuynh I just checked. super.onActivityResult() is the first line inside ProfileContentFragment's onActivityResult() method. – Chandan Pednekar May 10 '18 at 08:46
  • Ok just to be sure because I don't see the `super` call on your code in the question. And 1 more question, do you happen to `finish` the activity after calling `startActivityForResult`. If no, then this code looks all good to me – Tam Huynh May 10 '18 at 08:53
  • @TamHuynh I call finish() method in ProfileVideoRecordingActivity after setResult(). Thats the only place I have called finish() in this scenario. I'll update the code to reflect super statement – Chandan Pednekar May 10 '18 at 08:56

0 Answers0