How do I get the currently displayed fragment?

前端 未结 30 1683
青春惊慌失措
青春惊慌失措 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:23

    What I am using to find current displaying fragment is in below code. It is simple and it works for me by now. It runs in the activity which holds the fragments

        FragmentManager fragManager = this.getSupportFragmentManager();
        int count = this.getSupportFragmentManager().getBackStackEntryCount();
        Fragment frag = fragManager.getFragments().get(count>0?count-1:count);
    
    0 讨论(0)
  • 2020-11-22 11:23

    If you are getting the current instance of Fragment from the parent activity you can just

    findFragmentByID(R.id.container);
    

    This actually get's the current instance of fragment that's populated on the view. I had the same issue. I had to load the same fragment twice keeping one on backstack.

    The following method doesn't work. It just gets a Fragment that has the tag. Don't waste your time on this method. I am sure it has it's uses but to get the most recent version of the same Fragment is not one of them.

    findFragmentByTag()
    
    0 讨论(0)
  • 2020-11-22 11:24

    There's a method called findFragmentById() in SupportFragmentManager. I use it in the activity container like :

    public Fragment currentFragment(){
        return getSupportFragmentManager().findFragmentById(R.id.activity_newsfeed_frame);
    }
    

    That's how to get your current Fragment. If you have custom Fragment and need to check what Fragment it is, I normally use instanceof :

    if (currentFragment() instanceof MyFrag){
        // Do something here
    }
    
    0 讨论(0)
  • 2020-11-22 11:24

    it's so simple, not that much code you need to write yourFragment.isAdded() or yourFragment.isVisible();

    I prefer isAdded(),both of them return boolean value use it in if condition and must initialize your fragment in onCreate() otherwise you will get null point exception.

    0 讨论(0)
  • 2020-11-22 11:25
    getSupportFragmentManager().findFragmentById(R.id.content_frame).getClass().getSimpleName();
    

    Well, I guess this is the most straight forward answer to this question. I hope this helps.

    0 讨论(0)
  • 2020-11-22 11:26

    Inspired by Tainy's answer, here is my two cents. Little modified from most other implementations.

    private Fragment getCurrentFragment() {
        FragmentManager fragmentManager = myActivity.getSupportFragmentManager();
        int stackCount = fragmentManager.getBackStackEntryCount();
        if( fragmentManager.getFragments() != null ) return fragmentManager.getFragments().get( stackCount > 0 ? stackCount-1 : stackCount );
        else return null;
    }
    

    Replace "myActivity" with "this" if it is your current activity or use reference to your activity.

    0 讨论(0)
提交回复
热议问题