A Brief Intro to Android Fragment

Contents

1, definition 

2, comparison of Fragment and C structure type

3, what files make a fragment in Android

4, References

 

1, Definition 

A fragment represents a behavior or a portion of user interface in an Activity. [That means a fragment can’t independently exist without an Activity which is to say a fragment is embedded visibly or invisibly into an Activity.]

You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity. [In an activity, you can integrate a couple fragments each of which can serve different goals.]

2, Comparison of Fragment and C structure type

Let’s compare the Fragment and a C structure type

A fragment element (from UI components’ viewpoint) A C structure definition
1, A TextView

2, A button

3, A Edittext

struct Books {

char title[20];

int book_id;

char author[20];

char subject[20];

}

As we can see, A fragment bundles a few UI components together and make a relatively customized UI interfaces. Definitely a fragment is more sophisticated than what I list in the above table.

3, What files make a fragment in Android

3.1 So in Android Studio when developing a fragment, two files coexist: .XML and .Java.

You can think of .XML is the outlook of the fragment, and .Java is the inside working mechanism of the fragment.

3.2 The .Java class must extend Fragment class and inside the .Java class we must override the onCreateView() method. Inside this method we must use Inflater.inflate() to connect the fragment layout (.XML file) with the Java code.

3.3 A lot more to go with the fragment within the .Java file, which will not be the scope of this briefly personal summary. For example,

How fragments can communicate with each other within the same activity?
How the activity interacts with one fragment?
How to make a fragment invisible?
How to building a flexible UI using fragment?

4, References

Official definition for Fragments: http://developer.android.com/guide/components/fragments.html
Youtube video: should go through 23—26 at least https://www.youtube.com/watch?v=dQ6uc__qP-g

One thought on “A Brief Intro to Android Fragment

Leave a comment