Get the fragment name or id
Solution 1
I found the solution
getSupportFragmentManager().beginTransaction()
.replace(R.id.framelayout,new MyFragment1())
.commit();
final Fragment fragmentInFrame = getSupportFragmentManager().findFragmentById(R.id.framelayout);
if (fragmentInFrame instanceof MyFragment1){
Log.d("debug", "you are in MyFragment1");
} else if (fragmentInFrame instanceof MyFragment2) {
Log.d("debug", "you are in MyFragment2");
}
Solution 2
You need to use below code to get runtime fragment from FrameLayout
Fragment fragmentInFrame = (Fragemnt)getSupportFragmentManager()
.findFragmentById(R.id.frag2_view);
then you have check fragment type using instanceof
if (fragmentInFrame instanceof MyFragment1){
Log.d("debug", "you are in MyFragment1");
}else if (fragmentInFrame instanceof MyFragment2){
Log.d("debug", "you are in MyFragment2");
}
Solution 3
You need to check using instance like
you need to check with your fragment object:
public Fragment getVisibleFragment(){
FragmentManager fragmentManager = MainActivity.this.getSupportFragmentManager();
List<Fragment> fragments = fragmentManager.getFragments();
if(fragments != null){
for(Fragment fragment : fragments){
if(fragment != null && fragment.isVisible())
return fragment;
}
}
return null;
}
fragment = getVisibleFragment();
if (fragment instance of MyFragment1){
}
Solution 4
I Have One ContentFrameLayout
this Id Is Fragment_Main
You Can Use FrameLayout
To Activity
Write This code
final Fragment fragmentInFrame = getSupportFragmentManager().findFragmentById(R.id.Fragment_Main);
if(fragmentInFrame instanceof YourFragment){
}
To Fragment
Write This Code
final Fragment fragmentInFrame =getActivity().getSupportFragmentManager().findFragmentById(R.id.Fragment_Main);
if(fragmentInFrame instanceof YourFragment){
}
Solution 5
final Fragment fragmentInFrame = getSupportFragmentManager().findFragmentById(R.id.framelayout);
getSupportFragmentManager().beginTransaction()
.replace(R.id.framelayout, new MyFragment1())
.commit();
You are replacing the fragment here. So the fragmentInFrame
will still refer to old fragment.
So after replacing you need to update fragmentInFrame
variable by finding it again.
fragmentInFrame = getSupportFragmentManager().findFragmentById(R.id.framelayout);
Now checking the fragmentInFrame
will work.
Don't forget to remove the final
keyword.
Comments
-
Jéwôm' over 1 year
I have a
framelayout
called one time in myActivity
and various time in otherFragment
(passing by the sameframelayout
of theActivity
).In my
MainActivity.class
, I need to know in what fragment is myframelayout
. For example, I need to know ifframelayout
is usingMyFragment1.class
orMyFragment2.class
.I need something like this (in this example, the
log
have to say me "you are in MyFragment1"):FrameLayout frameLayout = (FrameLayout) findViewById(R.id.framelayout); getSupportFragmentManager().beginTransaction() .replace(R.id.framelayout,new MyFragment1()) .commit(); if (framelayout.getname.equals("package.MyFragment1.class")) Log.d("debug", "you are in MyFragment1"); else if (framelayout.getname.equals("package.MyFragment2.class")) Log.d("debug", "you are in MyFragment2");
How can I do that?