How do I get the currently displayed fragment?

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

    it's so simple, not that much code you need to write yourFragment.isAdded() or yourFragment.isVisible();

    I prefer isAdded(),both of them return boolean value use it in if condition and must initialize your fragment in onCreate() otherwise you will get null point exception.

提交回复
热议问题