Alan
Alan

Reputation: 9471

Proper Usage of Android Fragment

I have been reading up on the Android API and it seems like Fragments are meant to sort of modularize an Activity. So if an Activity has a ListView and DetailView then it should be split into two separate fragments and have the Activity act as the master controller.

In my previous project that I worked on we were using Fragments kind of like children of the Activity.

For example: Let's say there is a AutomobileActivity that is designed to save automobile input data to the cloud. We have fragments like: SedanFragment TruckFragment SportsUtilityFragment

These fragments take up the entire view of the Activity and only one is displayed at a time. While these fragments use methods in the Activity to call common webservices like saving automobile information, getting car information. They also do different things like the Truck may have an additional entry to set the "Bed Size" and SportsUtilityFragment may have "Tow Limit" etc.

So in a way we are leveraging a lot of re-use and modularizing, but it's not exactly what the Android API is detailing. Is this a bad way to use Fragments?

Upvotes: 0

Views: 42

Answers (1)

Vaibhav Sharma
Vaibhav Sharma

Reputation: 2319

This is a very objective question and this will have a lot of different answers. In my opinion what you are doing is correct. The reason behind this is that they have a set of common webservices. If we go by the mvc approach they can all have the same controller(for calling webservices), a model class(Superclass- vehicle) with separate models which inherit from this vehicle model class. By doing this you can have an additional entry parameter which will be present in these models. Your view, the fragments can easily call the instance of these models. If you modularize in this manner, you will be making life very easy for yourselves.

Upvotes: 1

Related Questions