'How to use ACTION_OPEN_DOCUMENT_TREE without startActivityForResult?

I want to select a folder on my phone's SD card. At the moment I am doing this:

activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
    @Override
    public void onActivityResult(ActivityResult result) {
        Log.d(TAG, "Activity result!");
        if (result.getResultCode() == Activity.RESULT_OK) {
        }
    }
});

..

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
activityResultLauncher.launch(intent);

How do I get the folder back from the activity? What if I display another activity - how can I tell which activityresult is which?

Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
activityResultLauncher.launch(intent);

The old way of doing things makes sense to me (e.g. see here with request codes and intents), but I don't understand how this is done with the new API calls.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source