How do I get the currently displayed fragment?

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

    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()
    

提交回复
热议问题