Home > Software design >  proper way to pass data from registerForActivityResult to onCreate method
proper way to pass data from registerForActivityResult to onCreate method

Time:03-11

I have the onCreate method:

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        ...
        ...
srButton.setOnClickListener { view: View ->
    
   
 chooseImageGallery()

    if (theSelectedImage == null) {
      ....}
 
    val bitmap = (theSelectedImage.drawable as BitmapDrawable)?.bitmap
}

      
private fun chooseImageGallery() {
        val intent = Intent(Intent.ACTION_PICK)
        intent.type = "image/*"

        //setContentView(R.layout.selected_image) // DELETE THIS

        theSelectedImage = findViewById(R.id.the_selected_image)
        getResultCamera2.launch(intent)
    

}

private val getResultCamera2 =
        registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) {
            if (it.resultCode == Activity.RESULT_OK) {
                theSelectedImage.setImageURI(it.data?.data)
          

            }
        }

I want, to press the button and select an image from gallery. And show that image. With the above code I can do that.

But the problem is with the if (theSelectedImage == null). The theSelectedImage is null because it isn't updated in the onCreate method.

How can I update that?

Note, that I used:

 setContentView(R.layout.selected_image)
 theSelectedImage = findViewById(R.id.the_selected_image)

inside the chooseImageGallery in order to be able to initialize these.

So, what is a proper way to do that?

UPDATE

I deleted the setContentView(R.layout.selected_image). It seems I forgot this from another thing.

Now, theSelectedImage is not null , but when I call

val bitmap = (theSelectedImage.drawable as BitmapDrawable)?.bitmap

it gives :

java.lang.NullPointerException: null cannot be cast to non-null type android.graphics.drawable.BitmapDrawable

Also, is there a better way to handle those data transfers to the onCreate method?

CodePudding user response:

I can understand your issue, and I'd let you know what is going wrong right now and what a better solution would be.

What's wrong right now?

Your idea is correct, implementation is a bit wrong.

if (it.resultCode == Activity.RESULT_OK) {
           theSelectedImage.setImageURI(it.data?.data)
     }

This is correct, what you need to understand here is it.data?.data is an optional call you're making so this value can be null.

So when you try to this later, it throws an error

val bitmap = (theSelectedImage.drawable as BitmapDrawable)?.bitmap

Modify this to the follow

val bitmap = (theSelectedImage.drawable as? BitmapDrawable)?.bitmap

We have made it an optional cast now, so that should fix your error at least.

Recommendations

I'd recommend to make a separate function entirely to call and handle your result

fun handleImageResponse(intent: Intent?) {
// Do stuff here
}

And call this like this

if (it.resultCode == Activity.RESULT_OK) {
           handleImageResponse(it.data)
     }
  • Related