How to instantiate fragment class using class name instead of index

后端 未结 3 1319
无人及你
无人及你 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-20 06:24

    Define a 'tag' for the Fragment while adding it like

     getFragmentManager().beginTransaction().add(new Fragment(),"your_tag");
    

    And while referencing it use

    getFragmentManager().findFragmentByTag("your_tag");
    

    In most cases, you would like to use YourFragment.class.getSimpleName() as your tag.

提交回复
热议问题