How to get the fragment instance from the FragmentActivity?

后端 未结 2 1443
臣服心动
臣服心动 2020-11-27 06:49

I set the content view in the FragmentActivity, and the activity will create the fragment instance for me according to the class name specified in the layout file. But how c

2条回答
  •  爱一瞬间的悲伤
    2020-11-27 07:05

    To get the fragment instance in a class that extends FragmentActivity:

    MyclassFragment instanceFragment=
        (MyclassFragment)getSupportFragmentManager().findFragmentById(R.id.idFragment);
    

    To get the fragment instance in a class that extends Fragment:

    MyclassFragment instanceFragment =  
        (MyclassFragment)getFragmentManager().findFragmentById(R.id.idFragment);
    

提交回复
热议问题