Passing an Object from an Activity to a Fragment


I have an Activity which uses a Fragment. I simply want to pass an object from this Activity to the Fragment.

How could I do it? All the tutorials I've seen so far where retrieving data from resources.


Let's be a bit more precise:

My Activity has a ListView on the left part. When you click on it, the idea is to load a Fragment on the right part.

When I enter this Activity, an Object Category is given through the Intent. This Object contains a List of other Objects Questions (which contains a List of String). These Questions objects are displayed on the ListView. When I click on one item from the ListView, I want to display the List of String into the Fragment (into a ListView).

To do that, I call the setContentView() from my Activity with a layout. In this layout is defined the Fragment with the correct class to call. When I call this setContentView(), the onCreateView() of my Fragment is called but at this time, the getArguments() returns null.

How could I manage to have it filled before the call of onCreateView() ? (tell me if I'm not clear enough)


This question is tagged with android

~ Asked on 2012-03-29 19:19:10

The Best Answer is


Create a static method in the Fragment and then get it using getArguments().


public class CommentsFragment extends Fragment {
  private static final String DESCRIBABLE_KEY = "describable_key";
  private Describable mDescribable;

  public static CommentsFragment newInstance(Describable describable) {
    CommentsFragment fragment = new CommentsFragment();
    Bundle bundle = new Bundle();
    bundle.putSerializable(DESCRIBABLE_KEY, describable);

    return fragment;

  public View onCreateView(LayoutInflater inflater,
      ViewGroup container, Bundle savedInstanceState) {

    mDescribable = (Describable) getArguments().getSerializable(

    // The rest of your code

You can afterwards call it from the Activity doing something like:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment fragment = CommentsFragment.newInstance(mDescribable);
ft.replace(, fragment);

~ Answered on 2012-03-29 19:36:30


In your activity class:

public class BasicActivity extends Activity {

private ComplexObject co;

protected void onCreate(Bundle savedInstanceState) {

    co=new ComplexObject();
    getIntent().putExtra("complexObject", co);

    FragmentManager fragmentManager = getFragmentManager();
    Fragment1 f1 = new Fragment1();
            .replace(, f1).commit();


Note: Your object should implement Serializable interface

Then in your fragment :

public class Fragment1 extends Fragment {

ComplexObject co;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    Intent i = getActivity().getIntent();

    co = (ComplexObject) i.getSerializableExtra("complexObject");

    View view = inflater.inflate(R.layout.test_page, container, false);
    TextView textView = (TextView) view.findViewById(;

    return view;

~ Answered on 2014-09-26 10:09:10

Most Viewed Questions: