I've seen all the posts of TabHost + FragmentActivity on Google Groups and on StackOverflow and I'm still getting problems.
And I don't understand anything.
I'm following this tutorial for TabHost and FragmentActivity: http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
I have the following thing:
in a tab I load a CategoriaFragment.class
, where I load a ListView
and I set a ListView.OnItemClickListener
for it. When I click, I call this code:
CategoriaFragment fragmentnuevo = new CategoriaFragment();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
Bundle b = new Bundle();
b.putBoolean("flagSottoCategorie", true);
b.putSerializable("sottocategorielista", (Serializable)sottocategorielista);
fragmentnuevo.setArguments(b);
transaction.addToBackStack(null);
transaction.add(R.id.realtabcontent, fragmentnuevo, FragmentTAG); // FragmentTAG is the same tag that the current Fragment
transaction.commit();
and yes, I call the same type of Fragment because I need the same recurses but with another List
(sottocategorielista). This is not the error because I've tried to use another .class and it's the same error.
When I press back button, I get again my first ListView
but listeners are not available. So I click and nothing happens. I've used add
and replace
as well.
Moreover, when I try to change Tab, if add
is typed, I get several fragments in the same layout, if remove
is typed, I get only one fragment on onTabChanged, but when I get back to my Fragment, nothing is available. Moreover if I press back button, I get the java.IllegalStateException
saying
Fragment is already added: CategoriaFragment
What should I do? Everything is easier with TabActivity, which is deprecated now and I would like to do everything with Fragments.
This is how my onTabChanged
works (also available on the resource on the web)
public void onTabChanged(String tag) {
TabInfo newTab = this.mapTabInfo.get(tag);
if (mLastTab != newTab) {
FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
if (mLastTab != null) {
if (mLastTab.fragment != null) {
ft.detach(mLastTab.fragment);
}
}
if (newTab != null) {
if (newTab.fragment == null) {
newTab.fragment = Fragment.instantiate(this,
newTab.clss.getName(), newTab.args);
ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
} else {
ft.attach(newTab.fragment);
}
}
mLastTab = newTab;
ft.commit();
this.getSupportFragmentManager().executePendingTransactions();
}
}
Thanks in advanced.