George Udosen
George Udosen

Reputation: 936

Android activity startActivityForResult not being called

I am trying to get a file and pass that file to Firebase and store it in my storage bucket but for some reason my startActivityForResult isn't being called even after adding the attribute android:launchMode="singleTop" to the manifest file as suggested in another answer and also not using the kotlin static function call on my button click event that is supposed to start the intent. And note both are in the same activity!

Here is my button click handler:

override fun onCreate(savedInstanceState: Bundle?) {
   btnImage.setOnClickListener {
            val intent = Intent()
            intent.action = Intent.ACTION_GET_CONTENT
            // set the intent type
            intent.type = "image/*"
            // accept only local content
            intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)
            startActivityForResult(Intent.createChooser(intent, "Insert Picture"), PICTURE_RESULT)
        } 
}

And this is my startAtivityForResult function:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == PICTURE_RESULT && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                val imageUri : Uri = data.data!!
                val ref : StorageReference = FirebaseUtil.storageRef!!.child(imageUri.lastPathSegment!!)
                ref.putFile(imageUri)

               {


            } else {
                Log.d("IMAGE", resultCode.toString())
                Toast.makeText(this, "Upload failure", Toast.LENGTH_LONG).show()
            }

        }
    }

Upvotes: 0

Views: 96

Answers (2)

AskNilesh
AskNilesh

Reputation: 69671

Android activity startActivityForResult not being called

You need to call super.onActivityResult(requestCode, resultCode, data) inside onActivityResult

You have commented the code of your super.onActivityResult(requestCode, resultCode, data) inside onActivityResult

Remove // from your super.onActivityResult(requestCode, resultCode, data) inside onActivityResult

Also, Use this

 if (resultCode == Activity.RESULT_OK && requestCode == PICTURE_RESULT)

insread of this

if (resultCode == PICTURE_RESULT && resultCode == Activity.RESULT_OK)

SAMPLE CODE

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == PICTURE_RESULT) {
            if (data != null) {
                val imageUri : Uri = data.data!!
                val ref : StorageReference = FirebaseUtil.storageRef!!.child(imageUri.lastPathSegment!!)
                ref.putFile(imageUri)

               {


            } else {
                Log.d("IMAGE", resultCode.toString())
                Toast.makeText(this, "Upload failure", Toast.LENGTH_LONG).show()
            }

        }
    }

Upvotes: 1

Eaweb
Eaweb

Reputation: 988

Change resultCode == PICTURE_RESULT to: requestCode == PICTURE_RESULT in your if statement.

Upvotes: 1

Related Questions