How do I get the currently displayed fragment?

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

    If you are using the AndroidX Navigation:

    val currentFragment = findNavController(R.id.your_navhost).?currentDestination
    

    For more info on this navigation component: https://developer.android.com/guide/navigation/navigation-getting-started

提交回复
热议问题