3

I am using android DrawerLayout from Support Library to show slide menu.I am using a single activity and 5-6 fragments to show them upon selection in DrawerLayout menu.But I have a small problem which is "How do I check which fragment is currently visible so if user selected the menu item which corresponds to already opened fragment. Currently it creating the Fragment again and displaying it which is not good.The function that triggers when clicked on menu item is:

private void selectItem(int position) {

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // Locate Position
        switch (position) {
        case 0:
            ft.replace(R.id.content_frame, fragment1);
            break;
        case 1:
            ft.replace(R.id.content_frame, fragment2);
            break;
        case 2:
            ft.replace(R.id.content_frame, fragment3);
            break;
        }
        ft.commit();
        mDrawerList.setItemChecked(position, true);
        // Close drawer
        mDrawerLayout.closeDrawer(mDrawerList);
    }

How do I check if the requested fragment is already opened so not to create it again?Is their any method to check this through FragmentManager?

Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
Ansh
  • 2,366
  • 3
  • 31
  • 51

4 Answers4

7

I would add to @Plato's answer. Check get currently displayed fragment question if you haven't already.

The answer says that when you add the fragment in your transaction, you can use a tag to represent a particular fragment. Something like:

ft.replace(android.R.id.content, fragment, "MY_FRAGMENT");

Later if you want to check if that fragment is visible, you can do something like:

RequestedFragment fragment = (RequestedFragment)getFragmentManager().findFragmentByTag("MY_FRAGMENT"); //"My_FRAGMENT" is its tag
if (fragment.isVisible()) {
   // add your code here 
}

Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
3

the simplest approach would be to store last used position and compare next time. Just make your selectItem method like:

int lastPosition = -1;
private void selectItem(int position) {

    if( position != lastPosition ) {

       [ old method body here ]

       lastPostion = position;
    }
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
3

Assign different tags to each one of your fragments and call findFragmentByTag() of the FragmentManager to check if each one is already present.

You can also use the findFragmentById() of the FragmentManager for the same purpose and use the ids of your fragments.

Plato
  • 2,338
  • 18
  • 21
0

If you are absolutely sure that each of fragment1, fragment2, fragment3 cannot be visible at the same time and if you reuse the same fragments (i.e. do not create a new fragment each time you change position) then the method isVisible() from the class Fragment should tell you if that fragment is currently visible.

Your switch should look like:

switch (position) {
        case 0:
            if(!fragment1.isVisible()) {
              ft.replace(R.id.content_frame, fragment1);
            }
            break;
        case 1:
            if(!fragment2.isVisible()) {
              ft.replace(R.id.content_frame, fragment2);
            }
            break;
        case 2:
            if(!fragment3.isVisible()) {
              ft.replace(R.id.content_frame, fragment3);
            }
            break;
        }
Julien Rousseau
  • 975
  • 1
  • 9
  • 15