Introduction: An Android library that holds fragment states for bottom navigation view even when activity rotates.
More: Author   ReportBugs   

An Android library that holds fragment states for BottomNavigationView. Saves fragment back stack even after activity rotation. Implementation derived from FragmentStatePagerAdapter.


Add Jitpack to project level gradle file

allprojects {
    repositories {
        maven { url "" }

Then add library to module level gradle file

dependencies {
  compile 'com.github.okaybroda:FragmentStateManager:1.+'


Create a FragmentStateManager instance in the OnCreate function of your Activity

FragmentStateManager fragmentStateManager;

protected void onCreate(Bundle savedInstanceState) {
    // ... your code ...
    FrameLayout content = findViewById(;
    fragmentStateManager = new FragmentStateManager(content, getSupportFragmentManager()) {
            public Fragment getItem(int position) {
                switch (position) {
                    case 0:
                        return new MainFragment();
                    case 1:
                        return new SearchFragment();
                    case 2:
                        return new MeFragment();

To change current fragment, call

fragmentStateManager.changeFragment(0); // 0 is the index of the fragment

Integration with BottomNavigationView

Add item selected listener

navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int position = getNavPositionFromMenuItem(item);
        if (position != -1) {
            return true;

        return false;

(Optional) Add item reselected listener. This will reset the fragment to it's original state

navigation.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
    public void onNavigationItemReselected(@NonNull MenuItem item) {
        int position = getNavPositionFromMenuItem(item);
        if (position != -1) {
Support Me
About Me
Google+: Trinea trinea
GitHub: Trinea