findFragmentByTag() returns null after perform a FragmentTransaction using replace() method

前端 未结 8 1703
广开言路
广开言路 2020-11-30 04:44

My Android app consists three fragments: A, B and C. They\'re loaded in the two containers defined in the MainActivity layout.

When th

8条回答
  •  眼角桃花
    2020-11-30 05:12

    Be sure you are adding or replacing the fragment in the proper way

    Next statement will add the fragment but it will return null when using getFragmentManager().findFragmentByTag(tag):

    transaction.add(R.id.mainContent, fragment);

    

    This way it will work;

    transaction.add(R.id.mainContent, fragment, tag);
    

提交回复
热议问题