How do I get the currently displayed fragment?

前端 未结 30 1734
青春惊慌失措
青春惊慌失措 2020-11-22 11:21

I am playing with fragments in Android.

I know I can change a fragment by using the following code:

FragmentManager fragMgr = getSupportFragmentManag         


        
相关标签:
30条回答
  • 2020-11-22 11:27

    Well, this question got lots of views and attention but still did not contained the easiest solution from my end - to use getFragments().

                List fragments = getSupportFragmentManager().getFragments();
                mCurrentFragment = fragments.get(fragments.size() - 1);
    
    0 讨论(0)
  • 2020-11-22 11:28

    Checkout this solution. It worked for me to get the current Fragment.

    if(getSupportFragmentManager().getBackStackEntryCount() > 0){
            android.support.v4.app.Fragment f = 
             getSupportFragmentManager().findFragmentById(R.id.fragment_container);
            if(f instanceof ProfileFragment){
                Log.d(TAG, "Profile Fragment");
            }else if(f instanceof SavedLocationsFragment){
                Log.d(TAG, "SavedLocations Fragment");
            }else if(f instanceof AddLocationFragment){
                Log.d(TAG, "Add Locations Fragment");
            }
    
    0 讨论(0)
  • 2020-11-22 11:28

    This is work for me. I hope this will hepl someone.

    FragmentManager fragmentManager = this.getSupportFragmentManager();  
            String tag = fragmentManager
                        .getBackStackEntryAt(
                        fragmentManager
                        .getBackStackEntryCount() - 1)
                        .getName();
                  Log.d("This is your Top Fragment name: ", ""+tag);
    
    0 讨论(0)
  • 2020-11-22 11:29

    The reactive way:

    Observable.from(getSupportFragmentManager().getFragments())
        .filter(fragment -> fragment.isVisible())
        .subscribe(fragment1 -> {
            // Do something with it
        }, throwable1 -> {
            // 
        });
    
    0 讨论(0)
  • 2020-11-22 11:30

    This is simple way to get current fragment..

    getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
      @Override public void onBackStackChanged() {
        currentFragment = fragmentManager.findFragmentById(R.id.content);
        if (currentFragment !=  null && (currentFragment instanceof LoginScreenFragment)) {
          logout.setVisibility(View.GONE);
        } else {
          logout.setVisibility(View.VISIBLE);
        }
      }
    });
    
    0 讨论(0)
  • 2020-11-22 11:34

    If get here and you are using Kotlin:

    var fragment = supportFragmentManager.findFragmentById(R.id.fragment_container)
    

    R.id.fragment_container is the id where the fragment is presenting on their activity

    Or if you want a nicer solution:

    supportFragmentManager.findFragmentById(R.id.content_main)?.let {
        // the fragment exists
    
        if (it is FooFragment) {
            // The presented fragment is FooFragment type
        }
    }
    
    0 讨论(0)
提交回复
热议问题