Ramz
Ramz

Reputation: 658

Got Null value during passing data from fragments to Activity

I'm working with fragments and encounter with a following problem :

  1. While passing data from fragments i used bundle to pass integer values and sending them through intent.
  2. I'm calling them by using intent .
  3. But while i'm printing then i'm getting only Null values.

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

Answers (4)

Jose Kurian
Jose Kurian

Reputation: 743

I think the following link can help you.

http://laaptu.wordpress.com/tag/android-passing-data-from-activity-to-fragment/

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

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

Blackbelt
Blackbelt

Reputation: 157447

You have to use (note the s at the end of the putExtras)

 in.putExtras(bundle);

otherwise you can not directly retrieve it with getIntent().getExtras();

Upvotes: 9

NaserShaikh
NaserShaikh

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

Related Questions