0

Hi I'm trying to pass a string variable from an activity into a fragment but it's always null.

Firstly I set the variable value, then create the bundle in the Activity:

String results = setResultCaption(bothEarsBad, leftEarBad, rightEarBad).toString();

Then

Bundle bundle = new Bundle();
bundle.putString("resultsString", results);
RightEarResults rightEarResults = new RightEarResults();
rightEarResults.setArguments(bundle);

I then call the bundle from the fragment onCreateView method as follows:

String bundle = getArguments().getString("resultsString");

And then set the variable in the TextView

txt = (TextView) rootView.findViewById(R.id.results_text);
txt.setText(bundle);

Can anyone help me understand why it's always null.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Paddy1990
  • 946
  • 4
  • 13
  • 30

3 Answers3

1

Provide a newInstance() method for your Fragment and hand over the parameter there:

public static YourFragment newInstance(String valueToPass) {

   YourFragment f = new YourFragment();

   Bundle b = new Bundle();
   b.putString("key", valueToPass);
   f.setArguments(b);

   return f;
}

In your Actitity:

getSupportFragmentManager().beginTransaction().replace(R.id.container, YourFragment.newInstance(stringtoPass), "yourFragTag").commit();

Inside your Fragment, you can then retrieve the value by using the getArguments() method:

String yourvalue = getArguments().getString("key");

EDIT : Also, please check if your setResultCaption(...) method actually returns something, and not NULL.

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • thanks for the help! so will I still have to call FragmentTransaction.add in the activity? – Paddy1990 Mar 15 '14 at 01:00
  • Yes you can use add instead of replace as well. – Philipp Jahoda Mar 15 '14 at 01:50
  • the part I don't really get is what I pass into 'replace(R.id.container, YourFragment.newInstance(stringtoPass), "yourFragTag")' what is the R.id.container, is it the textview i want to pass the string into? and "yourFragtag" is that just a string in my fragment? I created a string called 'public static final String IS_RIGHT_EAR = "is_right_ear";' is this what i pass in and do I bass the value or the reference? – Paddy1990 Mar 15 '14 at 09:19
  • R.id.container is the layout in your .xml file that will contain the Fragment. Usually, a FrameLayout is used for that. – Philipp Jahoda Mar 15 '14 at 09:27
  • "yourFragTag" is a tag for your Fragment, an identifier that will enable you to find your Fragment and access it. It is not really needed in your case. – Philipp Jahoda Mar 15 '14 at 11:29
  • The string is now passed into the interface on the fragment, but returns null at 'resultTxt = getArguments().getString("key");' how can that be? – Paddy1990 Mar 15 '14 at 17:41
0

If you want to pass variables on fragment initialisation, you can :

  • Use a bundle as parameter of Fragment.setArgument() method

  • Use a static method with your variables as parameters. Check @Philipp Jahoda's answer.

If you want to pass variables to fragment after initialisation, you can refer to this post from the official doc

S.Thiongane
  • 6,883
  • 3
  • 37
  • 52
0

You can just do:

From your activity;

Intent passIntent = new Intent(MainActivity.this, Yourclass.class);
passIntent.addExtra("resultsString",results);
startActivity(passIntent);

In your fragment;

Intent intentBundle = getActivity().getIntent();
        String someresult= intentBundle.getStringExtra("resultsString");
        Log.i("Result : ", someresult);

And then,

txt = (TextView) rootView.findViewById(R.id.results_text);
txt.setText(someresult);
mike20132013
  • 5,357
  • 3
  • 31
  • 41