- This is an Android Library that works with fragments and bottom navigation view.
- This library easily manages nested fragments, bottom navigation based fragments.
- Uses an Instagram like way to show, hide and remove fragments.
-
Add it in your root build.gradle at the end of repositories:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
-
Add the dependency
dependencies { implementation 'com.github.uysalemre:Fragment-Manager:0.1.0' }
-
In OnCreate method
-
Define your ViewGroup which is your FrameLayout
-
Define your BottomNavigationView
-
Call StateManager.buildInstance as shown below
-
Pass parameters which is ids for your bottom navigation items in builder
-
Pass ViewGroup as parameter in builder
-
Pass getSupportFragmentManager() as parameter in builder
-
You must be careful that call StateManager.buildInstance after findViewById's and before bottomNavigation.setOnNavigationItemSelectedListener
public class MainActivity extends AppCompatActivity { private ViewGroup fragments; private BottomNavigationView bottomNavigation; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragments = findViewById(R.id.frame_layout); bottomNavigation = findViewById(R.id.bottom_navigation_menu); ... StateManager.buildInstance(new StateManagerBuilder( R.id.navigation_accounts, R.id.navigation_explore, R.id.navigation_likes ) .withViewGroup(fragments) .withSupportFragmentManager(getSupportFragmentManager())); bottomNavigation.setOnNavigationItemSelectedListener(menuItem -> { switch(menuItem.getItemId()) { case R.id.navigation_accounts: StateManager.getInstance().showOnNavigationClick(R.id.navigation_accounts, new FragmentAccounts()); return true; case R.id.navigation_explore: StateManager.getInstance().showOnNavigationClick(R.id.navigation_explore, new FragmentExplore()); return true; case R.id.navigation_likes: StateManager.getInstance().showOnNavigationClick(R.id.navigation_likes, new FragmentLikes()); return true; } return false; }); bottomNavigation.setSelectedItemId(R.id.navigation_accounts); bottomNavigation.setOnNavigationItemReselectedListener(item -> { }); }
-
You can easily use showFragment method
-
StateManager will manage it in background
-
You must know that you need to give the id of your stream that you want to add, in this case R.id.navigation_explore
-
For example when you click navigation_explore in bottomNavigation you will see FragmentExplore2 like in Instagram
-
Parameter 1 : id of your stream
-
Parameter 2 : fragment you want to show
FragmentExplore2 exploreFragment2 = new FragmentExplore2(); StateManager.getInstance().showFragment(R.id.navigation_explore, exploreFragment2);
-
For example you have a back button like below
-
You can call fragmentOnBackPressed function like below
-
You will see FragmentExplore after use back_button when you click navigation_explore in bottomNavigation
-
Parameter 1 : id of your stream
back_button.setOnClickListener(v -> { StateManager.getInstance().fragmentOnBackPressed(R.id.navigation_explore); });
-
You can call removeAllFragmentStream like below to return to FragmentExplore
-
Parameter 1 : id of your stream
-
Parameter 2 : fragment you want to show
StateManager.getInstance().removeAllFragmentStream(R.id.navigation_explore, new FragmentExplore());
-
Warning this function will set instance to null
-
For example you must use this when your user wants to logout from application
-
If you are not using system.exit(0) in your activity to exit from application or your fragment container activity destroyed from somewhere in code you have to use this function onDestroy state because otherwise you will get Exception from onSavedInstanceState
StateManager.getInstance().removeAll();