Actionbar Tab Fragment Question


#1

I have put together an app with many of the designs from the book. The app has 2 main screens each a listview. Everything worked fine individually using the SingleFragmentActivity model. In each listview when a row is selected I initially was starting a new activity to handle the selected item’s display, modification, etc like in the book.

Then I got “smart” and figured I could do this with a fragment and it would be much faster, better, etc. So I ginned up some code as follows which worked like a champ:

FragmentManager mFragmentManager = getActivity().getSupportFragmentManager(); FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction(); ItemFragment mItemFragment = new ItemFragment(); mFragmentTransaction.replace(R.id.fragmentContainer, mItemFragment); mFragmentTransaction.addToBackStack(null); mFragmentTransaction.commit();

It put the current fragment on the backstack and started up the new one MUCH faster than running it in an activity via startActivity, and when the user navigated back it went back to the listview wonderfully. THis worked fine and dandy from both listview-oriented activities.

The table is now set for my real issue. Next I needed to pull both those “miniapps” together into the whole app, so I added support for swipeable tabs and moved away from singlefragmentactivity and had a new “mainactivity” setup the actionbar tabs and viewpageadapter as shown in the android sample app EfficientNavigation at developer.android.com/training/i … index.html.

Everything works pretty well because of the great structuring that was taught in the book to make fragments independent modules. I can swipe back and forth between the listviews fine and dandy. The problem is that there seems to be no way to have my fragments either start up other fragments, or add fragments when the listitem is selected. The android docs specifrically say the fragments mapped by the actionbar tabs cannot go on the backstack.

So for the moment I have had to go back and make the fragments invoked as a result of user selections on the listviews actually start activities for their fragments to run in, the old “slow” way.

Can any of you BNR android gurus direct me to a solution that will enable me to essentially create fragments that can then return to the proper spot in the tab view? I do not know if actionbarsherlock has a solution to this either. Also I find it quite baffling that the whole movement to a fragment-based tab architecture AWAY from activities actually forces you to go back to activities when the user’s actions within a fragment require substantial UI changes.

It seems like a 50-50 deal - in the old way using an activity for each tab item actually allowed you to build activity-related backstacks for each tab slot. Now we have traded in this flexibility and robustness for quicker switching between tabs on swipes I guess.

Anyway I’ve read a ton in all the usual places on the web, and tried a bunch of “solutions” others have tried and gotten nowhere. I am betting you guys have had this question at a seminar at some point. Any advice is much appreciated.

Once again thanks for getting me to this point. My app is in the final stages of integration and I am having a ball!