Tony Sherman
Tony Sherman

Reputation: 295

Android:Sending data back to MainActivity

I have 3 Activities: Main -> Drink -> DrinkAlcohol I move through them in that order, but I want to take the results from DrinkAlcohol and send them to Main.

In DrinkAlcohol I am using SetResult and in Main I am using onActivityResult. But I am stuck on the DrinkAlcohol page, what am I doing wrong?

DrinkAlcohol XML Button

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="24dp"
    android:layout_marginEnd="24dp"
    android:clickable="true"
    android:onClick="gotoMain"
    app:srcCompat="@drawable/ic_home_black_24dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

DrinkAlcohol gotoMain function:

fun gotoMain(view: View){
    val radioButtonID = mRg?.checkedRadioButtonId
    val rb = findViewById<RadioButton>(radioButtonID!!)
    val checkedValue = rb.text.toString().replace("%","").toDouble()
    val asu = mSize*checkedValue/60
    logDrink(asu)

    val intent = Intent(this, MainActivity::class.java)
    val returnIntent = this.intent
    returnIntent.putExtra("asu", asu)
    setResult(Activity.RESULT_OK, returnIntent)
}

MainActivity receiving Code:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == RETURN_DRINK_ACTIVITY) {
        if (resultCode == Activity.RESULT_OK) {
            val asu:Double  = data!!.extras.get("asu").toString().toDouble()
            updateDailyDrinks(asu)
            updateDailyBudget(asu)
            updateWeeklyBudget(asu)
            updateBAC(asu)

        }
    }
}

I would expect to get the MainActivity Page which would fire off a set of Toasts, but I actually get nothing I just remain at the DrinkAlcohol Page(Activity).

What am I missing?

Code that calls DrinkAlcohol:

fun getDrinkSize(view: View){
    val size: Double = view.getTag().toString().toDouble()
    Toast.makeText(this, "The Drink is $size", Toast.LENGTH_LONG).show()

    var intent = Intent(this,DrinkAlcoholActivity::class.java )
    intent.putExtra("size", size)
    startActivity(intent)
}

Upvotes: 1

Views: 6060

Answers (2)

Sơn Phạm
Sơn Phạm

Reputation: 129

setResult() does not move you to any other Activities.

Instead of setResult(Activity.RESULT_OK, returnIntent) you should do this

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);// If an instance of this Activity already exists, then it will be moved to the front. If an instance does NOT exist, a new instance will be created.
startActivity(intent);

And inyour MainActivity onCreate() you do

Bundle bundle= getIntent().getExtras();
if(bundle !=null){
// get your data here
}

You dont need onActivityResult(). The code is in java since i dont know kotlin, sorry :)

Upvotes: 2

diegoveloper
diegoveloper

Reputation: 103361

I see your error, you need to call startActivityForResult(intent, request_code) instead of startActivity(intent) in order to receive the response in onActivityResult method:

Try with this :

fun getDrinkSize(view: View){
    val size: Double = view.getTag().toString().toDouble()
    Toast.makeText(this, "The Drink is $size", Toast.LENGTH_LONG).show()

    var intent = Intent(this,DrinkAlcoholActivity::class.java )
    intent.putExtra("size", size)
    startActivityForResult(intent,RETURN_DRINK_ACTIVITY)
}

Upvotes: 1

Related Questions