Receive incorrect resultCode in Activity's onRequestPermissionsResult when request permission from Fragment

Viewed 2065

I have an app with targetSdkVersion = 23, compileSdkVersion = 23 with main activity setup like following

- HomeActivity (AppCompatActivity)
  - FragmentA (V4 Fragment)
    - ViewPager
      - NestedFragmentA (V4 Fragment)
      - NestedFragmentB (v4 Fragment)
      - NestedFragmentC (v4 Fragment)
      - NestedFragmentD (v4 Fragment)
  - Fragment B (V4 Fragment)
  - Fragment C (V4 Fragment)

And in HomeActivity

public static final String PERMISSION = Manifest.permission.WRITE_EXTERNAL_STORAGE

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    Log.i("Logger", "Request Code: " + String.valueOf(requestCode));

    // Handle permission request result
}

I read this answer from @CommonsWare,

https://stackoverflow.com/a/33170531/760363

that the onRequestPermissionsResult in nested fragment will never gets called (in my case, it result will return to HomeActivity) but I'm fine with that, I'll manually notify fragment of the result.

But the problem is, when I request for permission in HomeActivity, everything works fine.

HomeActivity

// Request permission from HomeActivity
// Supply 101 as request code, get 101 back

@Override
public void clickSomething(View v) {
    requestPermissions(new String[]{PERMISSION}, 101);
}

// Logcat
Logger: Request Code: 101 <<< CORRECT

But in FragmetnA or NestedFragmentA, when I request from nested fragment, the requestCode that return to HomeActivity has changed

FragmentA

// Request permission from FragmentA
// Supply 102 as request code, get 358 back

@Override
public void clickAnotherThing(View v) {
    requestPermissions(new String[]{HomeActivity.PERMISSION}, 102);
}

// Logcat
Logger: Request Code: 358 <<< INCORRECT

NestedFragmentA

// Request permission from NestedFragmentA
// Supply 103 as request code, get 615 back

@Override
public void clickDifferentThing(View v) {
    requestPermissions(new String[]{HomeActivity.PERMISSION}, 103);
}

// Logcat
Logger: Request Code: 615 <<< INCORRECT

Do you have any idea what would cause this issue?

1 Answers
Related