Android Development: Troubleshooting Nested Fragment

Screen Shot 2015-03-20 at 9.27.54 AM

I am working on an Android app which takes the menu data from API display the dinner, drink, dessert menu and allows a customer to add items they are interested in into a ‘cart’ for the wait staff to look at and discuss the item the customers are interested in.

I’m using the project as a learning experience.  In the last week I was struggling with getting Android Fragments to work properly because I have nest Fragments.  The app has a main Activity, with a tab viewer, 3 Fragments and Fragments inside those 3 Fragments.   My app would often crash when I swipe back and forth and it looked to me like the Fragment::onCreateView() was called multiple times which was what the Fragment lifecycle was suppose to do.

I traced this down to the Android core code dealing with Fragments and thought maybe the problem is with the v4 API.

I have tried these 3 routes and I’ve finally found the fix this morning

  1. Replace the default Android Tab layout with PagerSlidingTabStrip ref: Android Guides from CodePath
  2. Use code to create the Fragment instead of using layouts, this didn’t work either
  3. Finally from this stackOverflow article (link), a gem that mentions Nested Fragments are support now at version 4.2+ Android SDK

What I’ve learned

  1. StackOverflow is very useful but there are conflicting answers
  2. The Android SDK is complex because there are so many backward versions, but having the Android source code to step through is invaluable. Also the main documentation did not talk about Nested Fragments being supported.
  3. Having Gradle to manage dependencies is fantastic

When I’ve found things to be useful, I have been adding the to my Android bundle