Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the current fragment object

People also ask

How do I get the currently displayed fragment?

To get the current fragment that's active in your Android Activity class, you need to use the supportFragmentManager object. The supportFragmentManager has findFragmentById() and findFragmentByTag() methods that you can use to get a fragment instance.

How do I get the current fragment from supportFragmentManager?

FragmentManager fragMgr = getSupportFragmentManager(); FragmentTransaction fragTrans = fragMgr. beginTransaction(); MyFragment myFragment = new MyFragment(); //my custom fragment fragTrans. replace(android. R.

How do I get NavHostFragment in activity?

Actually, verify that the NavHostFragement id in XML matches up with the one you are refering to in your code. Show activity on this post. If you want to inflate NavHost via Fragment then use this code in app. on this code ID MUST BE SAME AS NAVIGATION.


Now at some point of time I need to identify which object is currently there

Call findFragmentById() on FragmentManager and determine which fragment is in your R.id.frameTitle container.

If you are using the androidx edition of Fragment — as you should in modern apps — , use getSupportFragmentManager() on your FragmentActivity/AppCompatActivity instead of getFragmentManager()


Try this,

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

this will give u the current fragment, then you may compare it to the fragment class and do your stuffs.

    if (currentFragment instanceof NameOfYourFragmentClass) {
     Log.v(TAG, "find the current fragment");
  }

I think you can use onAttachFragment event may be useful to catch which fragment is active.

@Override
public void onAttachFragment(Fragment fragment) {
    // TODO Auto-generated method stub
    super.onAttachFragment(fragment);

    Toast.makeText(getApplicationContext(), String.valueOf(fragment.getId()), Toast.LENGTH_SHORT).show();

}

I think you should do:

Fragment currentFragment = fragmentManager.findFragmentByTag("fragmentTag");

The reason is because you set the tag "fragmentTag" to the last fragment you have added (when you called replace).


You can get the list of the fragments and look to the last one.

    FragmentManager fm = getSupportFragmentManager();
    List<Fragment> fragments = fm.getFragments();
    Fragment lastFragment = fragments.get(fragments.size() - 1);

But sometimes (when you navigate back) list size remains same but some of the last elements are null. So in the list I iterated to the last not null fragment and used it.

    FragmentManager fm = getSupportFragmentManager();
    if (fm != null) {
        List<Fragment> fragments = fm.getFragments();
        if (fragments != null) {
            for(int i = fragments.size() - 1; i >= 0; i--){
                Fragment fragment = fragments.get(i);
                if(fragment != null) {
                    // found the current fragment

                    // if you want to check for specific fragment class
                    if(fragment instanceof YourFragmentClass) {
                        // do something
                    }
                    break;
                }
            }
        }
    }