Reputation: 658
I'm working with fragments and encounter with a following problem :
From fragment
:
Bundle bundle = new Bundle();
bundle.putInt("myData", x);
Intent in=new Intent(getActivity(),B.class);
in.putExtra("xy", bundle);
startActivity(in);
In Activity
:
Intent in=getIntent();
Bundle bundle = getIntent().getExtras();
int value = bundle.getInt("myData");
Log.v("in mainactivity",""+value);
Here it getting Null values. I hope u understand the problem.
Upvotes: 3
Views: 10096
Reputation: 743
I think the following link can help you.
http://laaptu.wordpress.com/tag/android-passing-data-from-activity-to-fragment/
Upvotes: 0
Reputation: 132972
if you are passing Bundle
using Intent.putExtra
then get it as in second Activity :
Bundle bundle = getIntent().getBundleExtra("xy"); //<< get Bundle from Intent
int value = bundle.getInt("myData");//<extract values from Bundle using key
Upvotes: 2
Reputation: 157447
You have to use (note the s at the end of the putExtra
s)
in.putExtras(bundle);
otherwise you can not directly retrieve it with getIntent().getExtras();
Upvotes: 9
Reputation: 1576
instead of:
Bundle bundle = new Bundle();
bundle.putInt("myData", x);
Intent in=new Intent(getActivity(),B.class);
in.putExtra("xy", bundle);
startActivity(in);
you can simply pass data by:
Intent in=new Intent(getActivity(),B.class);
in.putExtra("myData", x);
startActivity(in);
Upvotes: 1