Reputation: 251
I have a background as an iOS developer and I am currently porting an app from iOS to Android using Xamarin. In this regards, I have some question regarding the view hierarchi on Android.
The attached UML diagram shows a subset of the interaction between fragments for the app. I want my view controller classes to have limited responsibility, encapsulating the functionality in a neat and clear way by nesting fragments (equivalent to container view controllers on iOS).
However, I was told by an Android developer, that nesting fragments is bad due to a severe performance degradation. I don't care about a small performance degradation, but the app should of course be responsive. We're only targeting newer tablets and phones.
Should nesting of fragments be avoided on Android? And what is the performance degradation, if any? (~1 ms for the example given here: http://developer.android.com/training/improving-layouts/optimizing-layout.html)
Update: The layout of the app is shown below. The functionality inside the ModelControllerFragment is rather complicated, since 2D/3D models are loaded and model manipulations can be done (in full screen) and the flow from ImageEditFlowActivity is used from several places in the app. The CommentListFragment also has some non-default behavior. Having all this functionality inside one fragment is not desirable.
Upvotes: 1
Views: 3568
Reputation: 516
I think this is what you need - ChildFragmentManager
Main problem in nesting fragments is working with this fragments from parent Fragment. Main issue is find correct fragment in container by id. When project is so huge it is can create a lot of bugs. You can try to imagine when
Fragment1 -> open Fragment2 -> which open Fragment3 -> which have to sent event to Fragment1
This logic is not easy to implement if we want to show the next fragment in the container of current layout.
Related to your class diagram ChildFragmentManager is the best practice to work with nested fragments.
Upvotes: 5
Reputation: 429
Without knowing too many more details it's hard to give you an alternative.
Do not nest Fragments, it will make your life hard. Debugging Fragments can be a difficult task as your application grows and managing the lifecycle is not the easiest of things (really depends on what all your application attempts to do). So, make life easier on yourself and do not nest fragments.
You should use the Design Support Library that are now provided easily. TabLayout for instance is a nice way to have 3 fragments available at the "same" time.
You can have a Fragment that adds other fragments to the stack - that is perfectly legal and reasonable thing to do.
Anyway, without more details it's hard to give additional suggestions. Though, once thing I can say - having done Xamarin development for both iOS and Android - do not attempt to try to create an Android App using the same UI model that you do for iOS. It won't work and it will make your head hurt. Android UI is much simpler to work with, don't over-complicate it.
UPDATE BASED ON EDITS ON OP: Is your view typically for tablets? Also, I don't see why OrderItemFragment needs to exist - it's simply a layout with components within. I also think the main issue here is that you aren't understand the use of Fragments completely. OrderListFragment, for instance seems like a good candidate for Master/Detail design. Your Detail pane looks like a good candidate for a Single Fragment with several Views or components within. I don't see why you would want nested Fragments here or what that would accomplish. You should really consider looking at leveraging RelativeLayout and LinearLayout to accomplish the task of how your View should look and then populate them using a single Fragment.
Upvotes: 2