3

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.

Garg
  • 2,731
  • 2
  • 36
  • 47
Ramz
  • 658
  • 1
  • 7
  • 19

4 Answers4

9

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();

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • awsomee...IQ thanq soo muchhhhh – Ramz May 28 '13 at 12:57
  • why we not use `Intent.putExtra` for passing data between components ? – ρяσѕρєя K May 28 '13 at 13:02
  • @ρяσѕρєяK we can. I was only pointing out that if he want to retrieve the bundle with getExtras he has to use the putExtras. Your answer is correct as my (sorry for the misunderstanding) – Blackbelt May 28 '13 at 13:08
  • Hi guys....thanx for your answer..I want to ask you another question...hope u guys can help me for this....Thnaq so much for sharing your technical skills....you can refer this link....http://stackoverflow.com/questions/16805678/fragment-was-unable-to-replace-with-fragment-in-viewpager – Ramz May 29 '13 at 04:48
2

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
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Hi guys....thanx for your answer..I want to ask you another question...hope u guys can help me for this....Thnaq so much for sharing your technical skills....you can refer this link....stackoverflow.com/questions/16805678/… – Ramz May 29 '13 at 05:52
1

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);
NaserShaikh
  • 1,576
  • 2
  • 23
  • 39
  • Hi guys....thanx for your answer..I want to ask you another question...hope u guys can help me for this....Thnaq so much for sharing your technical skills....you can refer this link....stackoverflow.com/questions/16805678/… – Ramz May 29 '13 at 05:53
0

I think the following link can help you.

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

Jose Kurian
  • 703
  • 7
  • 10