Hello World

2018-03-24 17:04:21 ARCHIVES

Here is a proper way to implement Back Navigation for Navigation Drawer in Android Studio by using Backstack. Every Android device comes a back button. It is one of the cool feature that Android has. Unlike iPhone having only home button where you can go back to previous state easily, Android remember your last state, last page and last fragment.

However, it comes with a price to pay. Fragment in Android can appear on any activity, where you can easily see it on Navigation Drawer (You can watch all the tutorials of Navigation Drawer in YouTube channel Oum Saokosal by clicking this link).

HOW TO GO BACK TO HOME FRAGMENT?

Sometimes in a real word app, you don’t want to go back to any previous Fragment. Instead, you want just to go back to a Home Fragment. This way is quick to browse. Here is the code.
First you need to clear all elements (Fragments) from the the BackStack by using a code line below:

Once you clear all, you can add just only one Fragment, the HomeFragment:

That is it. Now you can implement back navigation properly in the Navigation Drawer.