数据从父片段传递到选项卡片段(子) - Android

时间:2018-01-15 16:18:41

标签: java android android-fragments tabs

我想将数据从Parent Fragment传递给Tabbed Fragment(子)以进行初始数据传递(当父片段第一次加载时)我使用了Bundle数据。当在子片段中的嵌套片段中时,我调用PopbackStack()以便返回按钮单击。在那里我需要更新之前传递到子选项卡片段的包数据。捆绑数据没有获得更新的数据集

父片段 - 初始状态

 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewPager viewPager = (ViewPager) getView().findViewById(R.id.pager);
        CropFragAdapter adapter = new CropFragAdapter(getChildFragmentManager());



        Fragment tabfragment1 = new CropAlreadyPlantedFragment();
        Fragment tabfragment2 = new FuturePlantedFragment();


            Bundle b = new Bundle();
            b.putString("RAWJsonArr", INITAL_DATA_FOR_TAB_1);
            tabfragment1.setArguments(b);

            b.putString("RAWJsonArr", INITAL_DATA_FOR_TAB_2);
            tabfragment2.setArguments(b);



        adapter.addFragment(tabfragment1, "Already Planted");
        adapter.addFragment(tabfragment2, "Future Planted");


        viewPager.setAdapter(adapter);

        TabLayout tabLayout = (TabLayout) getView().findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);

    }

儿童片段 - 标签片段

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        cropAlreadyPlantedModel = new CropAlreadyPlantedModel();
        String RAWJsonArr =null;
        Bundle b = getArguments();
        if (b != null) {
            RAWJsonArr = b.getString("RAWJsonArr");
            JSONOBJ = null;
            try {
                DATASET = new JSONArray(RAWJsonArr);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }


        View view = cropAlreadyPlantedModel.getCropItems(inflater, container, getContext(), DATASET);
        return view;
    }

儿童 - >儿童片段按钮单击

 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            saveBtn = (Button) getView().findViewById(R.id.button);
            saveBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    JSONObject obj = new JSONObject();
                    try {
                        obj.put("crop_details",DATASETARRY);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    if(SELECTED_TAB ==0) {
                        childDataCropDetails = obj; //static variable in Parent Fragment
                    }
                    else if(SELECTED_TAB ==1){
                        Future_childDataCropDetails = obj; //static variable in Parent Fragment
                    }
                    FragmentManager manager = getActivity().getSupportFragmentManager();


                    manager.popBackStack();

                }
            });


        }

我想念的是什么?请帮忙

1 个答案:

答案 0 :(得分:0)

问题是我在Child Fragment中谈论的范围是错误的(不是当前范围)。

Bundle b = this.getArguments();