Communication between Fragment-Get dynamic Fragment object

Since dynamic Fragment does not add id, general communication uses Activity to indirectly manipulate components, instead of directly obtaining Fragment.
You can also send information by broadcasting to operate.
But I found that all Fragment objects can be obtained, which resulted in a method of obtaining dynamically added Fragments.

Defect: The Fragment to be obtained must be used only once (there is only one object in an Activity).

1. Normal operation method

https://blog.csdn.net/u012702547/article/details/49786417
不做赘述,直接介绍强行获取Fragment对象的方法。

2. Get the Fragment object

first step.

Fragment->Activity, the method is the same as above, omitted.

Step 2.

	// 从所有Fragment中查找唯一的RightFragment对象,注意:要获取的Fragment一定要唯一。
    List<Fragment> fragments = fragmentManager.getFragments();
    for (Fragment f: fragments) {
    
    
        if (RightFragment.class.isInstance(f)) {
    
    
            fragmentRight = f;
            break;
        }
    }
    TextView tv_fra_right = fragmentRight.getView().findViewById(R.id.tv_fraright);
    tv_fra_right.setText(mes + "再传到RightFragment中。");
    if (fragmentRight != null) {
    
    
        tv_activity = fragmentRight.getActivity().findViewById(R.id.tv_fraright);
        LogUtil.d("FragmentActivity showProMes", "使用了fragment对象。");
    }
    // 三件套必须一起用才稳键。
    fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.lin_fra_right, fragmentRight);
    fragmentTransaction.commit();

Zhuang Zhouxiao dreams of butterflies, and Wang Dichun cares for the cuckoo. ——Li Shangyin

Guess you like

Origin blog.csdn.net/weixin_37627774/article/details/109000858
Recommended