Streetboy
Streetboy

Reputation: 4401

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.

Upvotes: 3

Views: 6297

Answers (3)

Jon O
Jon O

Reputation: 6591

You can use Fragment.setRetainInstance(true) for this. See this related question.

Upvotes: 0

haythem souissi
haythem souissi

Reputation: 3273

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");

Upvotes: 6

207
207

Reputation: 3804

You can prevent the Activity from beeing destroyed due to orientation change. Add the following line to your manifest of your TabActivity (orientation change + keyboard visibility changes)

android:configChanges="keyboardHidden|orientation"

See here for description.

setArguments/getArguments does not help you here. As he stated you may use it to save instance variables and retrieving them after orientation change. But with the given line above you do not need that because your Activity/Fragments won't get destroyed

Upvotes: -3

Related Questions