25

Here is my code that is not working:

// Sending bundle this way:

        String topUser = String.valueOf(scores.get(arg2));

        Bundle data = new Bundle();
        data.putString("userprofile", topUser);

        FragmentTransaction t = getActivity().getSupportFragmentManager()
                .beginTransaction();
        SherlockListFragment mFrag = new ProfileFragment();
        mFrag.setArguments(data);
        t.replace(R.id.main_frag, mFrag);
        t.commit();

// Retrieving this way:

        Bundle extras = getActivity().getIntent().getExtras();
        userName = extras.getString("userprofile");

Basically, the data isn't received. Am I on the right track or is there a better way of doing this?

TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259

1 Answers1

32

You should be using the getArguments() method of the Fragment class. So put something like the following inside your Fragment:

Bundle extras = getArguments();

Reference: http://developer.android.com/reference/android/app/Fragment.html#getArguments()

Tushar
  • 8,019
  • 31
  • 38
  • 5
    just to clarify - calling `getActivity().getIntent().getExtras()` actually works, but you get the "arguments" of the underlying `Activity` and not the `Fragment`s :) – Mykhailo Gaidai Mar 11 '13 at 00:32
  • 1
    I cant tell you how long I just spent trying to figure out how to get that from the saved state bundle... I guess because I also happened to be passing a parcelable object — anyway this should be starred – roberto tomás Apr 04 '16 at 19:11