Reputation: 21
I have an framelayout which loads 6 different fragment. Is there any way to find the name of the current fragment in framelayout from activity.
Fragment1
getFragmentManager().beginTransaction().replace(
R.id.main,Fragment.instantiate(LoadingScreen.this,
"com.myapp.fragments.fragment1",bundle)).commit();
Fragment2
getFragmentManager().beginTransaction().replace(
R.id.main,Fragment.instantiate(LoadingScreen.this,
"com.myapp.fragments.fragment2",bundle)).commit();
I need to know which fragment is live in the framelayout, from activity. Since two different fragments are loaded in the same layout.
Upvotes: 0
Views: 4092
Reputation: 19
You can use findFragmentById()
it will return the current fragment in the container.
Upvotes: 1
Reputation: 465
If you want to know which fragment is loaded in the layout, you can do something like this:
Fragment f = getSupportFragmentManager().findFragmentById(R.id.main);
if(f instanceof Fragment1){
//do something
}
else if(f instanceof Fragment2){
//do something
}
If you want to know class name then use:
String name = f.getClass().getCanonicalName()
Upvotes: 2