Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get an ActivityResultRegistry reference from an Activity?

Tags:

java

android

I'm trying to get this example code to work in Java:

https://developer.android.com/training/basics/intents/result

private final ActivityResultLauncher<Void> mTakePicture =
        registerForActivityResult(new TakePicturePreview(), mRegistry, new ActivityResultCallback<Bitmap>() {
            @Override
            public void onActivityResult(Bitmap thumbnail) {
                mThumbnailLiveData.setValue(thumbnail);
            }
    });

The example happens to be a "Fragment", and it gets mRegistry from the constructor:

public class MyFragment extends Fragment {
    private final ActivityResultRegistry mRegistry;
    ...
    public MyFragment(@NonNull ActivityResultRegistry registry) {
        super();
        mRegistry = registry;
    }

My test case is an Activity (the "MainActivity"), not a Fragment:

public class MainActivity extends AppCompatActivity {
    ...
    private  ActivityResultRegistry activityResultRegistry;

Q: How can I initialize my registry ("activityResultRegistry") in this scenario?

like image 815
paulsm4 Avatar asked Oct 28 '25 06:10

paulsm4


1 Answers

It was easier than I thought:

private ActivityResultRegistry activityResultRegistry = this.getActivityResultRegistry();
like image 112
paulsm4 Avatar answered Oct 31 '25 00:10

paulsm4