Google deprecated the OnActivityResult, here are three different ways to use the new ActivityResultLauncher – Android Development Tips
Using it as an image picker
ActivityResultLauncher<String> imageLauncher = registerForActivityResult( new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() { @Override public void onActivityResult( Uri uri ) { // Handle the returned Uri if( uri == null ) return; // do something with the uri } } );
Calling the imageLauncher – Android Development Tips
private void getFromGallery() { imageLauncher.launch( "image/*" ); }
————————————————————————————————————-
Get information back from an activity,
the values are passed back from the activity
First, Call the activity with the data it needs – Android Development Tips
private void processActivity() { Intent intent; intent = new Intent( this, MyActivity.class ); intent.putExtra( "some_value", some_value); intent.putExtra( "another_value", another_value ); // etc ... getSomeInfoLauncher.launch( intent ); }
Then in the activity, pass back the info required. – Android Development Tips
private void sendStuffBack() { Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putInt( "intResult", intResult); bundle.putString( "stringResult", stringResult ); intent.putExtras( bundle ); setResult( Constants.SOME_RESULT_CODE, intent ); finish(); }
And here is the ActivityResultLauncher – Android Development Tips
ActivityResultLauncher<Intent> getSomeInfoLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if( result.getResultCode() == Constants.SOME_RESULT_CODE ) { Intent intentData = result.getData(); if( intentData == null ) return; Bundle bundle = intentData.getExtras(); int intValue = bundle.getInt( "intValue" ); String stringValue = bundle.getString( "stringValue" ); // etc ... // do something with results } );
————————————————————————————————————
Taking a Photo requires a lot more work
Android Development Tips
Edit your AndroidManifest.xml file and add the following under the application tag:
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileProvider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />; </provider>;
Next create an XML file under xml in res – Android Development Tips
<paths> <cache-path name="cached_files" path="." /> <files-path name="images" path="." /> <external-path name="files_root" path="Android/data/path.to.your.app/" /> <external-path name="external_storage_root" path="."/> </paths>
To call the launcher, create a member variable to hold a Uri – Android Development Tips
private Uri takePhotoUri = null;
Then use code similar to the following – Android Development Tips
private void takePhoto() { String fileName = "some_prefix_" + some_suffix; File outputDir = getCacheDir(); File file; try { file = File.createTempFile( fileName, ".jpg", outputDir ); } catch( IOException e ) { return; } try { takePhotoUri = FileProvider.getUriForFile( Objects.requireNonNull( getApplicationContext()), BuildConfig.APPLICATION_ID + ".fileProvider", file ); } catch( IllegalArgumentException e ) { return; } try { takePhotoLauncher.launch( takePhotoUri ); } catch( Exception ignore ) { } }
Here is the ActivityResultLauncher – Android Development Tips
ActivityResultLauncher<Uri> takePhotoLauncher = registerForActivityResult( new ActivityResultContracts.TakePicture(), result -> { if( !result ) return; // do something with uri (takePhotoUri) } );