Skip to content Skip to sidebar Skip to footer

What Is The Best Way To Add Fragment In The Tabs Android

My application had a bottom navigation bar which has 5 tabs. So according to these tabs, I have 5 fragments When I click on the tab, the fragment changed according to that tab. I c

Solution 1:

for adding fragment I make this code for my project, hope it will be help.

publicstaticvoidreplaceFragment(Fragment fragment, FragmentManager fragmentManager) {
        StringbackStateName= fragment.getClass().getName();
        StringfragmentTag= backStateName;


        FragmentcurrentFrag= fragmentManager.findFragmentById(R.id.frame_container);
        Log.e("Current Fragment", "" + currentFrag);

//        boolean fragmentPopped = fragmentManager.popBackStackImmediate(backStateName, 0);intcountFrag= fragmentManager.getBackStackEntryCount();
        Log.e("Count", "" + countFrag);


        if (currentFrag != null && currentFrag.getClass().getName().equalsIgnoreCase(fragment.getClass().getName())) {
            return;
        }


        FragmentTransactionft= fragmentManager.beginTransaction();
//        if (!fragmentPopped) {

        ft.replace(R.id.frame_container, fragment);
        ft.addToBackStack(backStateName);
        ft.commit();
//        }

        currentFrag = fragmentManager.findFragmentById(R.id.frame_container);
        Log.e("Current Fragment", "" + currentFrag);


    }

hope this will be help you, and use this method in entire project for replacing fragment.

Solution 2:

Using ViewPager with FragmentPagerAdapter suits for you in this case.

Then use ViewPager#setOffsetPageLimit(5). This will help you show/hide your fragments without recreating it again.

Follow this tutorial

Let try it, then tell me if your problem is solved or not. ;)

Solution 3:

you don't have to need to hide the fragment just replace the fragment like this method:

publicvoidsetFragment(Fragment fragmentWhichYouWantToShow) {

        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        ft.replace(R.id.container, fragmentWhichYouWantToShow);
        ft.commit();

}

Solution 4:

Try to make it in a singe transaction.

protectedvoidshowAsRootFragment(Fragment fragment, @NonNull String tag) {
    FragmentManagersupportFragmentManager= getSupportFragmentManager();        
    FragmentTransactiontransaction= supportFragmentManager.beginTransaction();
    if (supportFragmentManager.getFragments() != null) {
        for (Fragment attachedFragment : supportFragmentManager.getFragments()) {
            if (attachedFragment != null && !tag.equals(attachedFragment.getTag())) {
                transaction.hide(attachedFragment);
                attachedFragment.setUserVisibleHint(false);
            }
        }
    }

    if (!fragment.isAdded()) {
        transaction.add(R.id.frame_container, fragment, tag);
        fragment.setUserVisibleHint(true);
    } else {
        transaction.show(fragment);
        fragment.setUserVisibleHint(true);
    }

    transaction.commit();
}    

Post a Comment for "What Is The Best Way To Add Fragment In The Tabs Android"