Skip to content Skip to sidebar Skip to footer

How To Save State Of Fragment In Navigation Drawer

I'm using google navigation drawer library from the following link... https://github.com/Arasthel/GoogleNavigationDrawerMenu See code below: mDrawer.setOnNavigationSectionSelec

Solution 1:

Instead of creating new fragment every time you can keep an array of fragments and just return the same object:

mDrawer.setOnNavigationSectionSelected(new GoogleNavigationDrawer.OnNavigationSectionSelected() {

Fragment[] fragments ={new HomeFragment(),new NewsFragment(),  new TransferFragment(), new BlogsFragment(),new FixturesFragment() , new BestXIFragment(),new FeedFragment()};
 @Override

    public void onSectionSelected(View v, int i, long l) {

        if (fragment[i] != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.main, fragments[i]).commit();

        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }
});

Post a Comment for "How To Save State Of Fragment In Navigation Drawer"