0

I am creating an app that uses a navigation drawer in its main activity. in the drawer the user can navigate through 8 different fragments. I am currently using

fragmentTransaction.replace(R.id.container,MyFragment)

to navigate through the fragments but it is not the best for the user experience because it reloads the fragments each time and the navigation drawer sliding is not fluid. I tried using

fragmentTransaction.add(R.id.container,MyFragment)

and then hiding all others fragment but it becomes confusing and it does not work very well especially when a fragment is called from another fragment. is there a way to make the navigation more fluid without the need to reload the fragment each time it is selected?

Ivano Donadi
  • 377
  • 1
  • 13
  • I don't think it's a good idea mantaining 8 fragments in memory. I'd go with container activities, so 1 fragment 1 activity, or something like that. – Jumpa Jul 21 '15 at 10:11
  • could you please explain better? – Ivano Donadi Jul 21 '15 at 10:13
  • You could use an activity as a parent and the inside it instantiate a particular fragment (1 out of 8). This way when you navigate the drawer you launch a new activity (with replicated drawer). The solution depends on the semantic correlation between fragments. If they are atomic, isolated try the above solution, if they are somehow related you could think about using tabs and view pager with option setOffscreenPageLimit(3). – Jumpa Jul 21 '15 at 10:18
  • Both the solutions (view pager and activities) normally show one fragment at a time, so you don't need to hide the others. If you'll go with view pager and have some code that should be executed when a specific fragment is visible to the user, you could use onPageSelected (from ViewPager.OnPageChangeListener). – Jumpa Jul 21 '15 at 10:26

4 Answers4

1

I would suggest to use fragmentTransaction.add(R.id.container,MyFragment) for avoiding reloading issue and maintain a check before adding which will check whether your fragment(which is you are going to add) is already exits in back stack or not if yes then pick from back stack else add a new instance.

Adarsh Yadav
  • 3,752
  • 3
  • 24
  • 46
  • i know but handling 8 fragments using add with show and hide is quite confusing – Ivano Donadi Jul 21 '15 at 10:06
  • and i can do it in the main activity but how can i check if the fragments are added from inside the fragments? – Ivano Donadi Jul 21 '15 at 10:07
  • Put this function inside your base activity:- public void addFragmentWithBackStack(Fragment fragment, Fragment launcherFragment) { if (your check here) { } getSupportFragmentManager().beginTransaction() .add(R.id.mainFragmentContainer, fragment, fragment.getClass().getSimpleName()) .hide(getSupportFragmentManager().findFragmentByTag(launcherFragment.getClass().getSimpleName())) .addToBackStack(fragment.getClass().getSimpleName()) .commit(); } – Adarsh Yadav Jul 21 '15 at 10:12
  • @IvanoDonadi: Have look here for checking existing fragment- http://stackoverflow.com/a/15042122/1384010 – Adarsh Yadav Jul 21 '15 at 10:16
  • ok ill'try it. do you know a way to hide all the other fragments added without hiding them one by one? – Ivano Donadi Jul 21 '15 at 10:22
  • FragmentManager fm = getFragmentManager(); for(int entry = 0; entry < fm.getBackStackEntryCount(); entry++){ Log.i(TAG, "Found fragment: " + fm.getBackStackEntryAt(entry).getId()); } Using above code you will get list of all back stack fragments just write code for hiding them inside loop in place of log. – Adarsh Yadav Jul 21 '15 at 10:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83859/discussion-between-adarsh-yadav-and-ivano-donadi). – Adarsh Yadav Jul 21 '15 at 11:23
1

The best way to fragment transaction is using backstack... It will put the current fragment to backstack and load it when the user navigating to back... Check the sample code below...

FirstFragment firstFragment = new FirstFragment();
getSupportFragmentManager().beginTransaction().addToBackStack(null)
                .add(R.id.content_frame, firstFragment).commit();

if u want a smooth fragment transaction, u might have to use custom animation for fragment transaction... there are a plenty of tutorials available online...

And if you want to save the current state of the fragment check this answer...

Community
  • 1
  • 1
Rishad Appat
  • 1,786
  • 1
  • 15
  • 30
  • i know about the animations but the transaction is not smooth because each fragment has quite a lot of code to do and it reloads every time – Ivano Donadi Jul 21 '15 at 10:19
1

I used @Adarsh Yadav answer and some other answers found on the internet to make this function that solved my problems:

public void changeFragment(Fragment fragment)
 { 
    FragmentManager fragmentManager=getSupportFragmentManager();
   FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
     if (fragment.isAdded())
       {
           fragmentTransaction.show(fragment);

       }
       else
       {
           fragmentTransaction.add(R.id.container,fragment,fragment.getClass().getSimpleName()).addToBackStack(fragment.getClass().getSimpleName());

       }
    for (int entry=0;entry<fragmentManager.getBackStackEntryCount();entry++)
       {
           FragmentManager.BackStackEntry backStackEntry=fragmentManager.getBackStackEntryAt(entry);
           String simpleName=backStackEntry.getName();
           if (!simpleName.equals(fragment.getClass().getSimpleName())) {
               Fragment fragmentToHide = fragmentManager.findFragmentByTag(simpleName);
               fragmentTransaction.hide(fragmentToHide);
           }
       }
       fragmentTransaction.commit();
  }
Ivano Donadi
  • 377
  • 1
  • 13
0

try this:

FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, MyFragment).commit();

FragmentManager is useful when you want to change fragments in an Activity.

user3641702
  • 393
  • 1
  • 5
  • 18
  • i am using FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); so i am doing exactly the same thing – Ivano Donadi Jul 21 '15 at 10:01