A great user interface is a mandatory requirement these days in any mobile phone today. The increasing number of mobile phones and competition everywhere is resulting in innovation and creativity. People these days are no more interested in looking at the age-old icons and a traditional layout of web page. The tech freak generation wants something, which is unusual and trending.
Fragments enrich the user experience and provide a unique class to the process of android app development. Although these fragments are supported by Android 3.0, they can be used for creating even lower versions that support the designated libraries. They can help you to make many fragments within a single activity in order to make multi-pane user interface.
How Can Fragments Create a Dynamic User Experience?
Previously, all the screens of an Android had a separate activity thus making the process of transferring information from one screen to the other a difficult process. With the help of fragments, this cumbersome process has been reduced to a large extent. Now, all the fragments, exist in one activity and one can choose the desired area of entering the object reference.
Now, let us take a look at the various steps that are involved in making a dynamic UI
- Fragment Creation
To create a fragment, one needs to create a subclass for the main fragment class. It includes various call back methods starting from onCreated to onStop and onPause. There must be three methods for creating a fragment. This includes onPause, onCreate an onCreateView. In order to define the fragment, use the following code
public static class MyFragment extends Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
return inflater.inflate(R.layout.my_fragment, container, false);
- Adding The Fragment With Activity
Once you have created the fragment, you can select either of the two methods mentioned below which will help you to add the fragment to the activity. Let us take a look at them.
The first method is to mention the fragment in the default Activity layout. This can be done simply by inserting a code in the layout activity file. The other method is to add the fragment with the Activity during the runtime of the server. In order to do this, you need two kinds of classes – FragmentTransaction and FragmentManager.
- Communicating With Fragments
If it is mandatory to use the interface in the fragment class, then make use of it within the activity, Don’t forget to define the reusable fragments. You can connect each one of them with an activity, which is followed by associating them with an application logic. This is done by three methods. The first one is by defining an interface, then implementing it and then by delivering the message to a fragment.
Fragments offer an extraordinary power to the architecture of a mobile application. Most of the developers around the world are trying to make the mobile app a more interactive user experience and a fragment acts as a catalyst to enhance the functionality of a mobile app.