bendecoste
bendecoste

Reputation: 798

Values from returned bundle are always 0

I am working on an app with 3 activites. Activity 3 opens from 2, and 2 opens from 1. I would like to get two numbers that the user enters in activity 3 and handle them in activity 2.

This is the code I am currently using in activity 3 to bundle my numbers to be returned to #2:

@Override
protected void onPause(){
    super.onPause();
    Bundle bundle = new Bundle();
    bundle.putInt("param1", num1);
    bundle.putInt("param2", num2);

    Intent i = new Intent(this, Activity2.class);
    i.putExtras(bundle);

    setResult(ACTIVITY_END, i);


    finish();
}

And then in Activity #2:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    Bundle bundle = this.getIntent().getExtras();
    int num1 = bundle.getInt("param1"));
    int num2 = bundle.getInt("param2");
    //do something with ints
}

However, no matter what numbers are sent from #3 to #2, num1 and num2 are always 0.

Any ideas?

Thanks a lot!

Upvotes: 1

Views: 2438

Answers (2)

Rajdeep Dua
Rajdeep Dua

Reputation: 11230

You should put params directly in the intent

i.putExtra("param1", num1);
i.putExtra("param2", num2)

to retrieve these values use

int param1 = intent.getIntExtra("param1", -1);

link

Upvotes: 3

serkanozel
serkanozel

Reputation: 2927

Try this user, @Override

protected void onPause(){
    super.onPause();
    Bundle bundle = new Bundle();
    bundle.putInt("param1", num1);
    bundle.putInt("param2", num2);

    Intent i = new Intent(this, Activity2.class);
    i.putExtra("my_bundle", bundle);

    setResult(ACTIVITY_END, i);

    finish();
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    Bundle bundle = data.getBundleExtra("my_bundle");
    int num1 = bundle.getInt("param1"));
    int num2 = bundle.getInt("param2");
    //do something with ints
}

Upvotes: 3

Related Questions