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.