androidkotlinonactivityresultregisterforactivityresult

What object should I pass to a function that requires Void! in a parameter? (Kotlin)


I am using the contract ActivityResultContracts.TakePicturePreview() to capture a little image.

private val cameraLauncher =
        registerForActivityResult(ActivityResultContracts.TakePicturePreview()) { bitmap ->
                view?.findViewById<ImageView>(R.id.imageOutput)?.setImageBitmap(bitmap)
        }

When I try to launch the Activity for Result, I realise that this contract requires a Void! object as a input. So that, The only way I can launch this activity is passing "null" as a parameter, what I think is not very beautiful.

cameraLauncher.launch(null)

I've tried passing "Nothing", "Unit" but the type mismatch.

What is the right way to do so?

The header of that function would be

public void launch(Void input)

Solution

  • As per the Java docs,

    The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void

    Because the Void class can not be instantiated, the only value you can pass to a method with a Void type parameter, is null.

    The reason this is even required in the first place is because all the ActivityContracts inherit from the base abstract class ActivityResultContract<I, O> which expects two data types for input (I) and output(O). Since TakePicturePreview doesn't require any input, it uses Void? to just make up for that.

    Now to answer,

    What is the right way to do so?

    Passing null is the right (and the only) way here.