How do I get the currently displayed fragment?

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

    1)

    ft.replace(R.id.content_frame, fragment, **tag**).commit();
    

    2)

    FragmentManager fragmentManager = getSupportFragmentManager();
    Fragment currentFragment = fragmentManager.findFragmentById(R.id.content_frame);
    

    3)

    if (currentFragment.getTag().equals(**"Fragment_Main"**))
    {
     //Do something
    }
    else
    if (currentFragment.getTag().equals(**"Fragment_DM"**))
    {
    //Do something
    }
    

提交回复
热议问题