How to instantiate fragment class using class name instead of index

后端 未结 3 1288
无人及你
无人及你 2021-02-20 05:41

I have two fragment class named SessionTab and BillingTaband i am trying to create instance of those class using

SessionTab sessionTab          


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-20 06:21

    Use one of this methods : findFragmentById() and findFragmentByTag() methods.

    Reference : https://developer.android.com/reference/android/app/FragmentManager.html#findFragmentById(int)

    Update :

    ClassCastException is invoked when you are not casting the appropriate classes to one another. In your case, Your FragmentManager is returning different fragment than SessionTab, so the exception is thrown.

    If you use findFragmentById() or findFragmentByTag() , then it will return the fragment exactly what you want, and exception will not be thrown.

提交回复
热议问题