Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java solution for "startActivityForResult(Intent,int) in Fragment has been deprecated" when opening external URL?

My app contains a simple Fragment that is used to open external web pages, with:

Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
startActivityForResult(chooserIntent, RC_OPEN_URL);

And, when the result is returned (in my Fragment's onActivityResult(...)), the Fragment is popped off the backstack.

But, I'm now getting the new deprecation warning:

startActivityForResult(Intent,int) in Fragment has been deprecated

I've read the corresponding Getting a result from an activity documentation, but I'm not sure how to adapt the example they provide for my particular case.

I have discovered the ActivityResultContracts.StartActivityForResult class, but can't figure out how to pass my chooserIntent to it.

All the online examples for the class seem to be in Kotlin and I've not had any joy trying to decompile them to Java. So a Java example of how to use the new registerForActivityResult() method to open an external URL would be much appreciated.

like image 371
ban-geoengineering Avatar asked Jun 27 '20 17:06

ban-geoengineering


2 Answers

There's no reason to use startActivityForResult() at all for createChooser() - you can use startActivity and run your code from onActivityResult() immediately after you call to startActivity:

Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
startActivity(chooserIntent);
// Do your code from onActivityResult() here

However, if you really want to use the Activity Result API, then you can directly adapt the examples in the documentation, replacing the example GetContent contract with the StartActivityForResult contract:

// Create this as a variable in your Fragment class
ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(
    new StartActivityForResult(),
    new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {
            // Do your code from onActivityResult
        }
});

private void triggerChooser(Uri externalUri) {
    Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
    mLauncher.launch(chooserIntent);
}
like image 163
ianhanniballake Avatar answered Oct 31 '22 14:10

ianhanniballake


Below answer may help to someone.... But this is not readymade solution to above question.

I have faced lot of issue when I try to get result from activity to fragment. Finally I found below solution.

In side the fragment, I created ActivityResultLauncher.

var myActivityResultLauncher: ActivityResultLauncher<Intent> = registerForActivityResult<Intent, ActivityResult>(
        ActivityResultContracts.StartActivityForResult(),
        ActivityResultCallback<ActivityResult> {
            // ToDO: 
            if (it.resultCode == AppCompatActivity.RESULT_OK) {

            }
        }
) as ActivityResultLauncher<Intent>

And when I start the activity I used below code.

myActivityResultLauncher.launch(myIntent)
like image 44
Thirumalvalavan Avatar answered Oct 31 '22 14:10

Thirumalvalavan