Torben G
Torben G

Reputation: 790

Android with Kotlin - pass data back to previous Activity

I want to pass data from Activity 2 back to Activity 1 but it doesnt work. I tried to solve with onActivityResult, but it is never called.

This is my Acitvity 1 - ProductsActivity

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        Log.v("onActivityResult", "onActivityResult")
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1) {
            // 2
            if (resultCode == Activity.RESULT_OK) {
                // 3
                Log.v("onActivityResult", "onActivityResult")
            }
        }
    }

And here is my second Acitvity - the DetailsProductAcitivity

override fun onBackPressed() {
        super.onBackPressed()
        Log.v("onActivityResult", "on back pressed")
        val intent = Intent(this, ProductsActivity::class.java)
        startActivityForResult(intent, 1)
    }

Upvotes: 2

Views: 5528

Answers (5)

Mehul Kabaria
Mehul Kabaria

Reputation: 6622

In your Activity 2 you have to do like below

override fun onBackPressed() {
        Log.v("onActivityResult", "on back pressed")
        val data = new Intent()
        data.putExtra(INTENT_PRODUCT_CHANGED, productChanged)
        //startActivityForResult(intent, 1)
        setResult(Activity.RESULT_OK, data)
        finish()    
    }

you have to just remove super.onBackPressed()

Upvotes: 1

Torben G
Torben G

Reputation: 790

I have another problem.

The onActivityResult will be called now but the data is null. And the resultCode is 0.

Activity 1:

startActivityForResult(intent, 1)

And this

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if(requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            val resultString = data!!.getStringExtra(DetailsProductActivity.INTENT_PRODUCT_CHANGED)
        }
    }
}

Activity 2:

override fun onBackPressed() {
        super.onBackPressed()
        Log.v("onActivityResult", "on back pressed")
        val data = Intent()
        data.putExtra(INTENT_PRODUCT_CHANGED, productChanged)
        //startActivityForResult(intent, 1)
        setResult(Activity.RESULT_OK, data)
        finish()    
    }

Upvotes: 0

Rasel
Rasel

Reputation: 5734

Activity 1

startActivityForResult(intent, 1)

Activity 2

override fun onBackPressed() {
        super.onBackPressed()        
        val intent = Intent()
        intent.putExtra("Key",data)
        setResult(RESULT_OK, intent);
        finish()
}

Activity 1 again

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        Log.v("onActivityResult", "onActivityResult")
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                //get extra data from data intent here
            }
        }
}

Upvotes: 2

S-Sh
S-Sh

Reputation: 3873

You should use not

startActivityForResult(intent, 1)

but

setResult(RESULT_OK, intent);
finish();

Upvotes: 0

Some random IT boy
Some random IT boy

Reputation: 8457

It's not getting called because:

  1. You are not setting the result
  2. You look like are creating a new activity instead of finishing the current one (?)

In your first activity:

private fun launchNewActivity() {
    val requestCode = [yourCodeHere]
    val intent = Intent(this, ProductDetailsActivity::class.java)
    startActivityForResult(intent, requestCode)
}

In your Second activity

private fun passProductAsResult(product: Product) {
    val data = Intent()
    data.putExtra("product", product.toString());

    setResult(Activity.RESULT_OK, data);
    finish()
}

Back in your First Activity override the method onActivityResult

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
     if(resultCode != Activity.RESULT_OK) return
     when(requestCode) {
         [yourRequestCode] -> { yourTextView.text = data.getStringExtra("product"); }
         // Other result codes
         else -> {}
     }
}

Make sure the data type is either Serializable or Parcelable if you're using complex data types.

Upvotes: 10

Related Questions