Konstantin Milyutin
Konstantin Milyutin

Reputation: 12356

Set arguments of fragment from activity

I want to pass arguments from my activity to a fragment, embedded into the activity. Fragment is embedded statically in xml layout. I tried to call setArgument() like this:

setContentView(R.layout.detail_activity);
DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.detailFragment);
detailFragment.setArguments(getIntent().getExtras());

but it is already too late, because setArguments has to be called immediately after fragment's creation. The only was I see it to getArguments() and the change the bundle. Any better way?

Upvotes: 26

Views: 28090

Answers (3)

ShashankG
ShashankG

Reputation: 41

Another way to pass data to Fragment is as following:

//In DetailFragment (for Instance) define a public static method to get the instance of the fragment

public static final DetailFragment getInstance(Bundle data) {
    DetailFragment fragment = new DetailFragment();
    fragment.setArguments(data);
    return fragment;
}

And when attaching DetailFragment from inside Activity

Bundle data = new Bundle();
//Add data to this bundle and pass it in getInstance() of DetailFragment
fragmentTransaction.replace(R.id.frament_layout, DetailFragment.getInstance(data));

Upvotes: 4

user3989492
user3989492

Reputation: 51

You have two options here

  1. If you just need information in the activity's intent, then placing information from the intent into the fragment arguments just adds an unneeded step. You might just a well keep things simple and from your fragment call

    Bundle data = getActivity().getIntent().getExtras();
    
  2. If you need to add information that is not in the activity's intent then in you fragment create a no parameter constructor like:

    public DetailFragment() {
        this.setArguments(new Bundle());
    }
    

then in your activity you can add whatever arguments you need with code like:

    DetailFragment frg = (DetailFragment) getFragmentManager().findFragmentById(R.id.detailFragment);
    frg.getArguments().putBundle("key", data);

the point here is to use the existing bundle object rather than trying to call setArguments() after the fragment has been attached to the activity.

Upvotes: 5

Ifrit
Ifrit

Reputation: 6821

AFAIK, you can't use setArguments() like that when you embed the fragment within XML. If it's critical, you'd be better off dynamically adding the fragment instead. However if you truly want the fragment to be embedded via XML, there are different ways you can pass along that data.

  1. Have the Activity implement the fragment's event listener. Have the fragment then request the required parameters from the Activity at creation or whenever needed. Communication with Fragment
  2. Create custom attributes that can be embedded in xml along with the fragment. Then during fragment's inflation process, parse the custom attributes to obtain their data. Custom fragment attributes
  3. Create public setters in the fragment and have the activity use them directly. If it's critical to set them prior to the fragment's onCreate() method, then do it from the activity's onAttachFragment() method.

Upvotes: 45

Related Questions