Get the fragment name or id

18,144

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.

Share:
18,144
Jéwôm'
Author by

Jéwôm'

I like travel, music, coding, and a lot of things !

Updated on September 06, 2022

Comments

  • Jéwôm'
    Jéwôm' over 1 year

    I have a framelayout called one time in my Activity and various time in other Fragment (passing by the same framelayout of the Activity).

    In my MainActivity.class, I need to know in what fragment is my framelayout. For example, I need to know if framelayout is using MyFragment1.class or MyFragment2.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?