Set fragment arguments from activity

I want to pass the arguments from my activity to the fragment embedded in the action. The snippet is statically embedded in the xml layout. I tried calling setArgument () as follows:

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

but it's too late, because setArguments needs to be called right after the fragment is created. The only thing I saw was getArguments () and a package change. Any better way?

+19
android android-activity android-fragments
Aug 08 '13 at
source share
3 answers

AFAIK, you cannot use setArguments() like this when you embed a fragment in XML. If this is critical, you'd better add the fragment dynamically. However, if you really want the fragment to be embedded via XML, there are different ways to pass this data.

  • Ask the object to implement a fragment event listener. Ask the fragment to request the necessary parameters from the Activity when creating or when necessary. Link to fragment
  • Create custom attributes that can be embedded in xml along with the snippet. Then, during the inflation fragmentation process, analyze user attributes to get their data. Custom Fragment Attributes
  • Create public setters in the fragment and use them directly. If it is important to set them before the onCreate() fragment method, then do this from the onAttachFragment() method.
+32
Apr 22 '14 at 17:25
source share
— -

Here you have two options.

  • If you just need information in the intent of the activity, then placing information from the intent into the arguments of the fragment simply adds an unnecessary step. You can just keep things simple and from your call fragment

     Bundle data = getActivity().getIntent().getExtras(); 
  • If you need to add information that is not in the intention of the action, then create a constructor without parameters in your fragment, for example:

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

then in your activity you can add any arguments that you need with the code, for example:

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

here you should use the existing package object, and not try to call setArguments () after attaching the fragment to the activity.

+5
Aug 29 '14 at 8:23
source share

Another way to transfer data to a fragment is as follows:

 //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 an 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)); 
+4
Oct 06 '15 at 5:31 on
source share



All Articles