0

I fail to understand that why is it only me facing such a trivial issue . I googled around and couldn't find much .

My case is simple . I have a layout with a fragment .

<fragment
    android:id="@+id/tabs_fragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/header"
    android:layout_above="@+id/footer"
    class="com.uae.mopw.fragments.TabsFragment" />

I need to send arguments to this fragments , but I CAN'T SEEM TO FIGURE OUT HOW . Had I been making a fragment in the code , I would have had a chance to invoke setarguments before the fragment gets attached to the activity .

However now I dont think I can control what happens when this fragment get attached to the activity because it happens during the initialization of the activity itself .

I try randomly overriding onFragmentAttached and setting the arguments there , however I still couldn't get through with it

I get a Fragment already active exception when I try the above .

Help ?

My activity Oncreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String moduleName = null;

    if (Utils.getLanguage(this).equals("ar")) {
        setContentView(R.layout.activity_application_for_work_to_work_ar);
        isArabic=true;
        moduleName=ModuleNames.DISTANCE_MEASUREMENT_SERVICE_AR;

    } else {
        setContentView(R.layout.activity_application_for_work_to_work);
        isArabic=false;
        moduleName=ModuleNames.DISTANCE_MEASUREMENT_SERVICE;
    }

    /*init header*/
    new Header(this,HeaderTypes.HEADER_INTERNAL,moduleName);

    /*take out bundle from intent*/
    Bundle args = this.getIntent().getBundleExtra(ModuleNames.DISTANCE_MEASUREMENT_SERVICE);

    /*Obtain fragment reference*/
    fragment=(TabsFragment)getFragmentManager().findFragmentById(R.id.tabs_fragment);       
    fragment.setArguments(new Bundle());


}
Muhammad Ahmed AbuTalib
  • 4,080
  • 4
  • 36
  • 59

2 Answers2

0

Create a static method in your fragment for instanciating:

public static FooFragment newInstance(final String someValue) {
    final FooFragment fooFragment = new FooFragment();
    final Bundle bundle = new Bundle();
    bundle.putString("key", someValue);
    fooFragment.setArguments(bundle);
    return fooFragment;
}

Use FragmentTransaction for adding or replacing the fragment:

transaction.replace(R.id.fragment_foo, FooFragment.newInstance("myValue"));

The you can access the data in your fragment by calling

getArguments().getString("key");
Thomas R.
  • 7,988
  • 3
  • 30
  • 39
  • Why would i need to add the fragment , when it is already explicitly declared in my layout and would be initialized alongwith the activity ? . Please see my question again , the part where I pasted the xml. I have my fragment defined directly in the layout – Muhammad Ahmed AbuTalib Jan 29 '14 at 14:14
0

As far as I know you can' talk setArguments after the fragment is created. Thomas's answer works fine if you're adding fragments programatically but not if you're using an xml layout like you are. In your position I'd just get the fragment through a FragmentManager during your Activity's onCreate and then call a method on it to set the values you want. It's a different way of doing things, that's why I prefer to start my fragments programatically, not through xml, as the back stack is handled nicely for you too.

roarster
  • 4,058
  • 2
  • 25
  • 38