1

I have added 3 fragments to my Activity with

String name = "fragment1"; // and ..2 and ..3
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.content_frame, fragment, name);
fragmentTransaction.addToBackStack(name);
fragmentTransaction.commit();

The last added (third) Fragment now is visible on top. Now I want to resume to the first added Fragment. But how? I can find this Fragment with

FragmentManager fragmentManager = getSupportFragmentManager();
Fragment firstFragment = fragmentManager.findFragmentByTag("fragment1");

If I call fragmentManager.getFragments() I still can find all three Fragments.

How to bring firstFragment back to top, make it visible again?

almisoft
  • 2,153
  • 2
  • 25
  • 33
  • 1
    Try `fragment.getView().bringToFront()`: http://stackoverflow.com/a/7332294/3060087 – Price Aug 20 '14 at 20:11
  • Good advice. But after switching between two fragments several times I receive NullPointerException... – almisoft Aug 20 '14 at 20:31

3 Answers3

2

You can hide your 2nd and 3rd fragment and make your 1st fragment visible. So you'll have the effect that first fragment is shown on top and others are invisible.

solution:

Use the FragmentTransaction's show and hide method. Firs you need to find all the fragment and call the FragmentTransaction to show and hide 2nd and 3rd fragments.

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • 1
    OK, that makes sense. But is that the only solution? Why is there a method like bringToFront() or resume()? Have I misunderstood the use of fragments? – almisoft Aug 20 '14 at 20:06
  • @almisoft `bringToFront` are only used for view childs not in fragment. so let say you have relative layout with a lot of child on top of each other you can call `bringToFront` on a child to bring in front(z-axis) – Rod_Algonquin Aug 20 '14 at 20:08
0

Here's how I do it in my app when I want to switch to fragment:

FragmentTransaction transaction = getFragmentManager().beginTransaction();
if (fragment.isAdded())
{
    transaction.show(fragment);
}
else
{
    transaction.replace(R.id.container, fragment);
}
transaction.addToBackStack(null);
transaction.commit();

Note the use of show.

ajw
  • 499
  • 6
  • 6
0

It ended up with this:

/**
 * @param tag name of the fragment to resume and to bring to top
 * @return true if fragment has been found
 */
private boolean bringFragmentToTop(String tag) {

    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag(tag);

    if (fragment != null) {

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        for (Fragment f : fragmentManager.getFragments()) { 
            if (f == fragment)
                fragmentTransaction.show(f);
            else
                fragmentTransaction.hide(f);
        }   

        fragmentTransaction.commit();
        return true;
    }

    return false;
}
almisoft
  • 2,153
  • 2
  • 25
  • 33