Chris
Chris

Reputation: 337

How to hide all other fragments

Just for coding practice, I'm creating a small game.

Currently I added a optionmenu, and if a item is selected a new fragment is shown. Thanks working good.

My problem is that the game fragment should never be destroyed. Only hidden.

If I'm using my code below both frames (about and help) can exist at the same time. But if I use replace on R.id.container the game fragment will stop (there is a timer in there).

Here is my Java code:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            getActionBar().setDisplayHomeAsUpEnabled(false);
            getActionBar().setHomeButtonEnabled(false);

            getFragmentManager().popBackStack();

            return true;

        case R.id.action_about:
            if (getFragmentManager().findFragmentByTag("about_fragment") == null) {
                getActionBar().setDisplayHomeAsUpEnabled(true);
                getActionBar().setHomeButtonEnabled(true);

                // Gets the game fragement so it can be hidden
                Fragment fragment = getFragmentManager().findFragmentByTag("game_fragment");

                getFragmentManager().beginTransaction()
                                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                                    .add(R.id.container, new AboutFragment(), "about_fragment")
                                    .hide(fragment)
                                    .addToBackStack("about_fragment")
                                    .commit();

                return true;
            }

            return true;

        case R.id.action_help:
            if (getFragmentManager().findFragmentByTag("help_fragment") == null) {
                getActionBar().setDisplayHomeAsUpEnabled(true);
                getActionBar().setHomeButtonEnabled(true);

                // Gets the game fragement so it can be hidden
                Fragment fragment = getFragmentManager().findFragmentByTag("game_fragment");

                getFragmentManager().beginTransaction()
                                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                                    .add(R.id.container, new HelpFragment(), "help_fragment")
                                    .hide(fragment)
                                    .addToBackStack("help_fragment")
                                    .commit();

                return true;
            }

            return true;

        default:
            return super.onOptionsItemSelected(item);
    }

Here my XML with the container:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DoNothingGame"
tools:ignore="MergeRootFrame" />

I could ask in every case in the switch if the other fragments are shown and hide them, but I think code will blow up a LOT if I use, for example, 10 or 20 menu items.

So.. Is there a way to hide every fragment except the new one?

Thanks for your help!

Upvotes: 3

Views: 1989

Answers (1)

MeLean
MeLean

Reputation: 3421

Maybe a little bit late but I manage to implement it like this:

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    for (Fragment frag : fm.getFragments()){
        ft.hide(frag);
    }

    ft.commit();

Upvotes: 3

Related Questions