Issue
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?
Solution
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);
}
Answered By - Sambhav. K
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.