0

My app uses the startActivityForResult pattern. To account for the fact that I am calling startActivityForResult from a fragment I unmask the request code.

MyFragment.java

    Intent intent = new Intent(context, LoginActivity.class);
    startActivityForResult(intent, 1);

MainActivity.java

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    int unmaskedRequestCode = requestCode & 0x0000ffff;

    if (unmaskedRequestCode == 1) {
        success();
    }
}

The issue I am having is that this code works perfectly fine when my build.gradle used

implementation 'androidx.appcompat:appcompat:1.2.0' 

However, when I upgraded to

implementation 'androidx.appcompat:appcompat:1.3.1'

I started getting requestCodes that seemed random. In my testing it looked like you could work around this by instead calling startActivityForResult from the parent getActivity().startActivityForResult(intent, 1);. However, I would prefer to understand the issue better before I start changing code to account for this new version.

What changed in appcompat that causes the newer version to give different request codes than before?

Tyler V
  • 9,694
  • 3
  • 26
  • 52
Kenny Sexton
  • 301
  • 4
  • 14
  • 1
    You may want to consider using the new pattern that [replaces startActivityForResult](https://stackoverflow.com/questions/62671106/onactivityresult-method-is-deprecated-what-is-the-alternative) - then you don't have to deal with request/response codes at all. – Tyler V Sep 28 '21 at 01:42
  • 1
    ^That's actually what's causing the random request codes in the `Activity`, too, 'cause recent versions are rerouting the applicable `Fragment` methods through that new `Activity` result registry mechanism, which generates its own codes. Your actual request code is being tracked internally, but I think the only place it's accessible is in the `Fragment`, since `FragmentManager` is who's ultimately tracking the request info and handling the `onActivityResult()` dispatch to the `Fragment`s. – Mike M. Sep 28 '21 at 03:12

0 Answers0