In my AppCompatActivity, I declared an ActivityResultLauncher. I used registerForActivityResult() to create it and passed a LambdaExpression for the ActivityResultCallback.
However, I need a reference to an object in this ActivityResultCallback, which I have when calling launch().
Example code:
private final ActivityResultLauncher<Intent> launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
object.doSomething();
});
method() {
Object object = new Object();
launcher.launch(new Intent(MainActivity.this, OtherActivity.class));
}
I could just save the object in a private field, but is this what Android intents us to do?
I understand that Android does not want you to share objects between different activities. But here, the reference should only be kept in my MainActivity.class.
Any help is appreciated!