Home > Software design >  How to get the currently displaying fragment in Nav Controller
How to get the currently displaying fragment in Nav Controller

Time:04-12

I use a singe activity and embed multiple fragments into it. Now I would like to know within the single activity class, which Fragment is currently being displayed. How can I do this? I had a look at the solution from here How to know if a Fragment is Visible?

MyFragmentClass test = (MyFragmentClass) getSupportFragmentManager().findFragmentByTag("testID");
if (test != null && test.isVisible()) {
     //DO STUFF
}
else {
    //Whatever
}

But I don't know what to you for the parameter "testID". Further, I tried to implement the solution from Get the current fragment object

Fragment currentFragment = getActivity().getFragmentManager().findFragmentById(R.id.fragment_container);

But here I get the error message: "Cannot resolve symbol 'fragment_container'"

Does anyone have an idea how to get the name of the current Fragment that is being displayed when using a single activity and multiple fragments approach?

CodePudding user response:

I know there are already 2 answers on mine here but still have 1 more solution so I am writing it here. This is the code:

NavController navController = Navigation.findNavController(this, R.id.fragment);
        int id=navController.getCurrentDestination().getId();
      if(id==R.id.startGameFragment ){ // change the fragment id
          selectedPosition(0);

      }else if(id==R.id.gameFragment ){ // change the fragment id
          selectedPosition(1);

      }else if(id==R.id.endGameFragment ){ // change the fragment id
          selectedPosition(2);

      }

CodePudding user response:

Your 1st approach:

If you want to find fragment by tag you need to set the tag first. You do it while making transaction, for ex:

getSupportFragmentManager()
.beginTransaction()
.replace(R.id.container, SheetEditorScreen.class, args, "testID" //Here you set the tag
)

and then you will be able to get fragment by tag as you did:

getSupportFragmentManager().findFragmentByTag("testID");

Your 2nd approach:

If you want to get fragment by id you need to pass container's view id which you declare in your activity's layout xml file (set in setContentView(R.id.main_activity)):

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:app="http://schemas.android.com/apk/res-auto">

   <androidx.fragment.app.FragmentContainerView
       android:id="@ id/fragment_container" <--- here is the id
       android:layout_width="match_parent"
       android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

and then you can find the visible fragment with:

getSupportFragmentManager().findFragmentById(R.id.fragment_container);

CodePudding user response:

You can do something like this:

  1. Create a field of Fragment in your activity. Like this:
private Fragment mCurrentlyDisplayingFragment;
  1. Whenever changing the fragment, just pass that fragment object to that. Like this:
MyFragment fragment = new MyFragment();
// show it in the frame layout
mCurrentlyDisplayingFragment = fragment;

Now, whenever you are checking, just check this field. Done

Happy coding

  • Related