Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit of using Fragments in Android, rather than Views?

When developing for Android, you can set your target (or minimum) sdk to 4 (API 1.6) and add the android compatibility package (v4) to add support for Fragments. Yesterday I did this and successfully implemented Fragments to visualize data from a custom class.

My question is this: what is the benefit for using Fragments as opposed to simply getting a View from a custom object, and still supporting API 1.5?

For example, say I have the class Foo.java:

public class Foo extends Fragment {      /** Title of the Foo object*/     private String title;     /** A description of Foo */     private String message;      /** Create a new Foo      * @param title      * @param message */     public Foo(String title, String message) {         this.title = title;         this.message = message;     }//Foo      /** Retrieves the View to display (supports API 1.5. To use,      * remove 'extends Fragment' from the class statement, along with      * the method {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)})       * @param context Used for retrieving the inflater */     public View getView(Context context) {         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);         View v = inflater.inflate(R.layout.foo, null);         TextView t = (TextView) v.findViewById(R.id.title);         t.setText(this.title);         TextView m = (TextView) v.findViewById(R.id.message);         m.setText(this.message);         return v;     }//getView       @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {         if (container == null) {             return null;         }         View v = inflater.inflate(R.layout.foo, null);         TextView t = (TextView) v.findViewById(R.id.title);         t.setText(this.title);         TextView m = (TextView) v.findViewById(R.id.message);         m.setText(this.message);         return v;     }//onCreateView  }//Foo 

Both methods are very simple to create and to work with in an Activity that, say, has a List<Foo> to display (for example, programmatically adding each to a ScrollView), so are Fragments really all that useful, or are they just an over-glorified simplification of getting a View, such as through the code above?

like image 748
Phil Avatar asked Dec 23 '11 15:12

Phil


People also ask

What are advantages of fragments?

The Fragment class in Android is used to build dynamic User Interfaces and should be used within the activity. The biggest advantage of using fragments is that it simplifies the task of creating UI for multiple screen sizes. An activity can contain any number of fragments.

What are the advantages of using fragment compared to activity?

Advantages of fragments include code reuse and modularity (e.g., using the same list view in many activities), including the ability to build multi-pane interfaces (mostly useful on tablets). The main disadvantage is (some) added complexity.


2 Answers

The main reason to use Fragments are for the backstack and lifecycle features. Otherwise, custom views are more light weight and simpler to implement.

At first, I actually tried to build a phone/tablet app using custom views. Everything appeared to work across phones AND tablets, even switching from single panel to split panel. Where I ran into trouble was with the back button and life cycle. Since I was simply updating views manually...there was nothing keeping track of the history of views and their states. Therefore, the back button did not work as expected and it was difficult to recreate even the latest state during life cycle events, such as when rotating the app. To fix that, I had to wrap my custom views in fragments and use the FragmentManager so that the previous states would be saved and recreated.

I realized after answering that I posted to a similar question a year earlier: https://stackoverflow.com/a/11126397/618881

like image 149
Henry Avatar answered Sep 22 '22 17:09

Henry


I'd say Fragments are useful in two scenarios: if you split up views on some devices/orientations and show them in two activities and show all the content in one on other devices. That would be a use case if you go on a tablet or maybe even in landscape mode on a phone: e.g. you show the list of items and the details on one screen. on a phone or in portrait mode you just show one part.

Another use case are reusable views. So if you have some views that are visible on different activities and also perform some actions you could put this behaviour into a fragment and then reuse it. Obviously you could probably do that with custom widgets too.

I wouldn't see any reason for using Fragments for every View and I guess it would just be an overhead. I'm only using them in the first use case and I'd say here it is a simplification.

like image 27
Maria Neumayer Avatar answered Sep 23 '22 17:09

Maria Neumayer