第二次替换Fragment后无法访问元素

时间:2019-01-10 20:42:36

标签: java android android-studio android-fragments

我的应用程序中有2个片段。第一个片段替换了屏幕上的另一个片段,它收集数据并将其发送到上一个片段并返回第一个片段。但是一旦显示了片段,我就无法更改组件功能(例如edittext的文本),而当我尝试读取数据(edittext的gettext)时,它给出的数据就是edittext的第一种情况。

相关代码部分:

//Both Classes have this fnc:
private void setFragment(Fragment fragment)
    {
        FragmentTransaction fragmentTransaction  =  getActivity().getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.frame_layout,fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
//Part From 1st Class
fragmentSearch.fragmentAdd = FragmentAdd.this;// I give my current fragment's class to 2nd fragment for return 
setFragment(fragmentSearch);//  I put second fragment on the screen 

//Part from 2nd Class
public FragmentAdd fragmentAdd;
Bundle bundle = new Bundle();
                bundle.putString("BARCODE",result.getText());//transfer of gathered data
                fragmentAdd.setArguments(bundle);
                setFragment(fragmentAdd);//and go back



//onCreateView of fragmentAdd (1st Class)
    Bundle bundle = this.getArguments();
        if (bundle != null) {
            String A = bundle.getString("BARCODE");
            txProductName.setText(A);//It doesn't change anything and doesn't arises any error
}

0 个答案:

没有答案