如何在片段实例化后保留片段组件的值?

时间:2015-08-17 16:47:16

标签: android android-layout android-fragments

我有这个方法,当用户点击列表中的项目时调用该方法:

 @Override
    public void navigateToPackageComponent(int packageIndex) {
        Fragment fragment = null;

        if (formFragment == null) {
                formFragment = new PdfFormFragment(packageIndex, editPendingPackage);
            }           
            fragment = formFragment;

        //...... another instances 

        if (fragment != null) {
            FragmentTransaction fragmentTransaction =   getFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.container, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    }

即使片段在上述方法中只实例化一次,每次调用片段中的以下onCreateView方法。

      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,       
      Bundle savedInstanceState) {
        setHasOptionsMenu(true);

        rootView = inflater.inflate(R.layout.fragment_pdf_form, container, false);
        //....  
        topLayer = rootView.findViewById(R.id.containerLayout);
        //.....

        indexDefRootView = (LinearLayout) rootView.findViewById(R.id.componentsLayout);
        for (IndexDefView index : indexDefinitions) {
            View indexView = inflater.inflate(R.layout.pdf_form_item, null);
            TextView tv = (TextView) indexView.findViewById(R.id.label);
            tv.setText(index.getLocalizedName());
            EditText editText = (EditText) indexView.findViewById(R.id.value);
            editText.setTag(index.getId());
            editTextList.add(editText);

            indexDefRootView.addView(indexView);
            if (editPending) {
                editText.setText(loadValue(index));
            }

            String text = loadValueFromSavedIndexes(index);
            System.out.println(">>>> load indexes " + text);


            } catch (Exception e) {
                LogS.e(e);
            }

        }

        return rootView;
    }

那么如果第一次没有实例化片段,我怎么能保留片段的值呢?

最诚挚的问候,

0 个答案:

没有答案