How do I get the currently displayed fragment?

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

    When you add the fragment in your transaction you should use a tag.

    fragTrans.replace(android.R.id.content, myFragment, "MY_FRAGMENT");
    

    ...and later if you want to check if the fragment is visible:

    MyFragment myFragment = (MyFragment)getSupportFragmentManager().findFragmentByTag("MY_FRAGMENT");
    if (myFragment != null && myFragment.isVisible()) {
       // add your code here
    }
    

    See also http://developer.android.com/reference/android/app/Fragment.html

提交回复
热议问题