Wrong requestCode in onActivityResult

前端 未结 6 1124
离开以前
离开以前 2020-11-28 01:14

I\'m starting a new Activity from my Fragment with

startActivityForResult(intent, 1);

and want to handle the result in the Fragment\'s pare

6条回答
  •  青春惊慌失措
    2020-11-28 01:34

    Easier:

    Java: int unmaskedRequestCode = requestCode & 0x0000ffff

    Kotlin: val unmaskedRequestCode = requestCode and 0x0000ffff

    Check for the lower 16 bits, just unmask it doing a logical AND with the upper 16 bits zeroed

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        final int unmaskedRequestCode = requestCode & 0x0000ffff
    
        if(unmaskedRequestCode == ORIGINAL_REQUEST_CODE){
          //Do stuff
    
        }
    }
    

提交回复
热议问题