I'll jump straight to the point.
I'm making an application with has three tabs (MainFragment, FragmentCategories, FragmentList). What I want to do next is this:
From the second tab, FragmentCategories, I open up a certain category. Using getFragmentManager and FragmentTransaction, I open up a FragmentCategoriesList. From there, when I choose a certain object, I want to open it in yet another "subfragment".
Following a number of tutorials, MyFragmentCategoriesList uses a ListView layout.
context = getActivity().getApplicationContext();
view = inflater.inflate(R.layout.fragment_categories_list, container, false);
Later in the code, I initialize a ListView object which uses that same layout (R.layout.fragment_categories_list == R.id.frag_cat_list).
final ListView lv1 = (ListView) view.findViewById(R.id.frag_cat_list);**
lv1.setAdapter(new MyCustomBaseAdapter(context, searchResults));
Here's a snippet where I select an object:
...
data.putString("selection",test);
FragmentManager fragManager = myContext.getSupportFragmentManager();
FragmentTransaction fragTrans = fragManager.beginTransaction();
FragmentCategoriesDetail FragCatDetail = new FragmentCategoriesDetail();
FragCatDetail.setArguments(data);
fragTrans.add(R.id.frag_cat, FragCatDetail);
fragTrans.addToBackStack(null);
fragTrans.commit();
...
Anyway, the problem is this:
fragTrans.add(R.id.frag_cat, FragCatDetail);
In this case, my FragCatDetail works, but I can't go BACK to the FragmentCategoriesList. If I put R.id.listview (a relative layout xml containing two textviews), my Fragment opens WITHIN an object of a ListView, and I can go back to FragmentCategoriesList.
If I put R.id.frag_cat_list (which is, after all, the layout of the current fragment, FragmentCategoriesList), my app crashes with
java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
which seems logical because FragCatDetail is a Fragment with a relative layout.
My question is this: what can I do in order to have my FragCatDetail fragment work like in the first case (relative layout) AND go back to FragmentCategoriesList like in the second case (listview layout).
Thanks in advance! Much appreciated.
Figured out what was the problem.
FragmentManager fragManager = myContext.getSupportFragmentManager();
FragmentTransaction fragTrans = fragManager.beginTransaction();
FragmentCategoriesDetail FragCatDetail = new FragmentCategoriesDetail();
FragCatDetail.setArguments(data);
fragTrans.add(R.id.frag_cat, FragCatDetail);
fragTrans.addToBackStack(null);
fragTrans.commit();
Throughout the application, I used
getFragmentManager().popBackStack()
. Since I was going to a new fragment (FragmentCategoriesDetail), I was doing that by getSupportFragmentManager.
In my Main Activity, I overrode onBackPressed() and inside I was using
getFragmentManager().popBackStack();
where in fact I should have been using
getSupportFragmentManager().popBackStack();
at least for this specific fragment.
Once again, it is a story of a FragmentManager vs SupportFragmentManager. I hope I helped someone with this. :)