Android App Dev的新功能。
我试图设置可打包实现,以便将数据从一个片段发送到另一个片段。
数据是第一次发送的,但是当我回头查看并从recyclerview中选择另一条数据时,该片段仍保留来自先前捆绑软件的数据,并且第二次没有捆绑软件发送。

@Override
public void inflateTopicFragment(Course_Info course_info) {


    if (mTopicFragment ==null){
        mTopicFragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putParcelable(getString(R.string.intentCourse), course_info);
        mTopicFragment.setArguments(args);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_host, mTopicFragment,getString(R.string.tag_fragment_topic));
        transaction.commit();
        mFragmentTags.add(getString(R.string.tag_fragment_topic));
        mFragments.add(new FragmentTag(mTopicFragment, getString(R.string.tag_fragment_topic)));


    }
    else {

        mFragmentTags.remove(getString(R.string.tag_fragment_topic));
        mFragmentTags.add(getString(R.string.tag_fragment_topic));
    }
    setFragmentVisibilities(getString(R.string.tag_fragment_topic));


}


主题片段OnCreate

 @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate: ");
    Bundle bundle = this.getArguments();
    if (bundle != null){
        mCourse_info = bundle.getParcelable(getString(R.string.intentCourse));
        Toast.makeText(getContext(), mCourse_info.getCourseCode(), Toast.LENGTH_SHORT).show();
    }

}
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.d(TAG, "onCreateView: ");
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_blank, container, false);
    mTopics = dm.getTopics();
    for (Topic_Info topic_info: mTopics){
        if (mCourse_info.getCourseCode().equals(topic_info.getCourse().getCourseCode())){
            mSelectTopics.add(topic_info);
        }

    }


    RecyclerView courseList = view.findViewById(R.id.recyclerView);
    courseList.setLayoutManager(new LinearLayoutManager(getContext()));
    courseList.setAdapter(new TopicRecyclerViewAdapter(getContext(), mSelectTopics, this));


    return view;
}

最佳答案

尝试

if (mTopicFragment !=null){
        getSupportFragmentManager().beginTransaction().remove(mTopicFragment).commit();
    }
        mTopicFragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putParcelable(getString(R.string.intentCourse), course_info);
        mTopicFragment.setArguments(args);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_host, mTopicFragment,getString(R.string.tag_fragment_topic));
        transaction.commit();
        mFragmentTags.add(getString(R.string.tag_fragment_topic));
        mFragments.add(new FragmentTag(mTopicFragment, getString(R.string.tag_fragment_topic)));

        mFragmentTags.remove(getString(R.string.tag_fragment_topic));
        mFragmentTags.add(getString(R.string.tag_fragment_topic));

    setFragmentVisibilities(getString(R.string.tag_fragment_topic));

10-08 13:41
查看更多