How do I get the currently displayed fragment?

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

    final FragmentManager fm=this.getSupportFragmentManager();
    final Fragment fragment=fm.findFragmentByTag("MY_FRAGMENT");
    
    if(fragment != null && fragment.isVisible()){
          Log.i("TAG","my fragment is visible");
    }
    else{
          Log.i("TAG","my fragment is not visible");
    }
    

提交回复
热议问题