Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create fragments using setArguments/getArguments

I am creating tabs in my app using info from epidemian answer

I created main class which handles tabs, this class extends TabActivity and creates tabs:

    Resources res = getResources();
    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;  
    Intent intent;

    intent = new Intent().setClass(this, stackA.class);    
    spec = tabHost.newTabSpec("tab1").setIndicator("Tab 1",
                      res.getDrawable(android.R.drawable.ic_menu_search))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, stackB.class);
    spec = tabHost.newTabSpec("tab2").setIndicator("Tab 2",
                      res.getDrawable(android.R.drawable.ic_menu_search))
                  .setContent(intent);
    tabHost.addTab(spec);
    tabHost.setCurrentTab(0);

Then for every tab is created FragmentActivity and this gives possibility to create stack in every tab. My fragments is created:

protected void navigateTo(Fragment newFragment) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(R.id.content, newFragment);
    ft.addToBackStack(null);
    ft.commit();
}  

public class fragmentA extends Fragment{
    private LinearLayout ll;
    private FragmentActivity fa;

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

    fa = super.getActivity();
    ll = (LinearLayout) inflater.inflate(R.layout.fragmenta, container, false);
    Button next = (Button) ll.findViewById(R.id.button1);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            ((ActivityInTab) getActivity()).navigateTo(new fragmentB());
        }
    });
    return ll;
    }
}

This is working. What is wrong that when i am at tab 1 and fragment B (first fragment in stack is A), rotate my device and then it go back at fragment A.

epidemian said something about orientations and using setArguments/getArguments, but i am new to android programming so i don't really know how to do that:

Finally, if you need to survive orientation changes, it's important that your fragments are created using setArguments/getArguments. If you set instance variables in your fragments' constructors you'll be screwed. But fortunately that's really easy to fix: just save everything in setArguments in the constructor and then retrieve those things with getArguments in onCreate to use them.

like image 968
Streetboy Avatar asked Mar 15 '26 21:03

Streetboy


1 Answers

int x;

Bundle args = new Bundle();

args.putString("variable", x);

FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction ft = fragmentManager.beginTransaction();

Fragment fragment = Fragment.instantiate(getActivity(), MyFragment.class.getName(), args);

ft.replace(R.id.layout1, fragment);

ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

ft.addToBackStack(null);

ft.commit();

than in the fragment:

getArguments().getInt("variable");
like image 179
haythem souissi Avatar answered Mar 17 '26 11:03

haythem souissi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!