How to use ActivityResult API in fragment when Activity overrides `onActivityResult` method

239 Views Asked by At

I am using new ActivityResultApi with ReactNavtive.

ReactActivity overrides onActivityResult method.

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

My Fragment code

result = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
         //not getting call back here
    }

How can I get callback back here?

One solution I am working on is calling fragment's onActivityResult method from activity like this

class MainActivity: ReactActivity {
   ... 
   override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    
    val fragment = supportFragmentManager.findFragmentByTag(TAG)
    
    if(fragment is LoadingFragment){
        fragment.onActivityResult(requestCode, resultCode, data)
    }
    
}
   ...

What would be the better approach? How to prevent overriding onActivityResult method in our Activity?

0

There are 0 best solutions below