Bottom Navigation View With Fragments (No fragment re-creation).

Solving the problem of always creating fragment new instance

Bukunmi Aluko
4 min readJun 20, 2018

What is a fragment ?

A Fragment is a piece of an activity which enable more modular activity design. Its can be said to be a sub-activity.

An example of how two UI modules defined by fragments can be combined into one activity for a tablet design, but separated for a handset design. source: https://developer.android.com/guide/components/fragments

Multiple fragments can be used in a single activity, each fragment has it own layout and life-cycle. Learn more about fragment on google android developer guide page. follow link below.

Our sample app will look like this.

Using fragments was cool until i tried applying it to bottom navigation view. Each time i navigate to another fragment, when i returned back to the previous one, the fragment was re-created.

what if someone scrolled to the 70th item in the recycler view, navigates then navigates back ?

my brain asking me.

The fragment will be re-created, and the user will have to scroll once again to the 70th item. I don’t think i want to start messing with bundles and back stack problems

So i created a new project

Bottom Navigation Activity (Android studio).

After a successful build, i created 3 new fragments.

  1. Home Fragment (Inflated a recycler view in it)
  2. Dashboard Fragment (Contains a button an a textview)
  3. Notifications Fragment (Contains scrollable text)
Creating a new fragment (Android studio).

Note: I did not include fragment factory methods and interface callbacks, when creating the fragments.

I wanted to use toolbar, and customize the activity_main.xml UI to make it a little flexible, so i edited my styles.xml and activity_main.xml file.

To inflate the fragments in a frame layout, i had to inflate a content_main.xml layout file.

You can clone the project on github

After many search for the best way to solve the problem, i finally came up with a solution that works perfectly. In the main activity class, i created five global objects, and set the Home Fragment as the active fragment.

In onCreate, after setContentView, i hid two fragments and committed them to the fragment manager, but i didn't hide the first fragment that will serve as home.

To move between activities, i customized the BottomNavigationView.OnNavigationItemSelectedListener method to hide the active fragment, commit it, then set the current fragment as the active fragment and it works perfectly.

I also included an options menu to launch a new settings activity.

Note: to prevent the Activity from recreating itself when the back button is clicked, you have to make the launchMode your “fragment hosting class” or MainActivity.java class singleTop.

I customized each fragment. clone the project to see full code. The main solution is posted above already.

Glad it worked.
Video with my phone

NOTE

  1. The fragments retain their positions, even after i launched a new activity and went back.
  2. Screen orientation is set to portrait in the manifest file.
  3. This is just a quick work around, see link below for the official solution.

Hope this helps.

Thanks.

UPDATE !!!

Refer to this for the official solution.

https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample

--

--