What is fragment? It is a activity inside activity screen. In very simple terms, fragment is similar as an activity screen, but the only difference between them is that fragment cannot live outside its parent activity. Lets put it this way, we can have one activity screen like, let’s say, Home Activity and three fragments in it that we can display in the activity.

Why fragment, why not whole new screen?!? Well, it’s simple. Why to use more code and more layouts when we are displaying simple stuff like Recycler View (similar as the old List View)? Imagine fragment as a headless activity screen. As long the activity is active, the fragment is active too, but not vice versa.

In our case we will have three buttons for displaying three different fragments when button is clicked

Here is the example:

First we need main activity to hold our fragments. It will be called Home Activity – HomeActivity.java , one package fragments, for the fragments .java files, and fragment1.java, fragment2.java and fragment3.java .

For the Home Activity we will need activity_home.xml layout file and three separate layouts for the fragments fragment_1.xml, fragment_2.xml and fragment_3.xml

Also we need to update our colors.xml, styles.xml and strings.xml files with corresponding colors and strings.

Functionality explanations:
First we declare the three buttons and one Fragment object for later initialization.
In onCreate() we bind the buttons with their layout defined in activity_home.xml like btn1, btn2 and btn3.
For each button we must have functionality for calling wanted fragment, like for when one button is clicked the fragment in the lower part of the screen appears. It can be done with initialization of the previously declared fragment object with wanted fragment like fragment = new Fragmen1() where Fragment1 is our firs java file, or Fragment1.java. With this object we are using the FragmentTransaction transaction object for starting and replacing transaction.replace(R.id.frame, fragment) Home Activity fragment frame* with the newly called fragment, and we must transaction.commit() that change with. With other two buttons is the same but calling different fragments.
Finally for removing the fragment, we must override the onBackPressed() method with simple if(fragment is not null) {clear.fragmenet and set it null if another back is pressed} else {do what default back click do}

In next code example, is the explanation how functionalities are made inside the fragment and what does everything means
First of all, your fragment must implement Fragment interface so it can be treated as such.
For simple fragment we are using only two @Overriden methods. The firs one is onCreateView() for the view inflater to take place with the fragment layout that we declare as fragment_fragment1.xml or in other words to show the layout of the called fragment. Second is onViewCreated(), imagine this as onCreate() in activity, or here magic happens. In in you can add whatever functionality you like or need. In my exercise I have only one Toast message for showing that it works. Everithing else is in the layout for the fragment.

And the layout of the fragment – fragment_fragment1.xml

And optional for your colors and strings:
strings.xml

colors.xml

styles.xml

You can get the whole project on GitHub BuktopMKD/

 

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *