查看可见性未从后台堆栈恢复

时间:2015-08-23 11:26:39

标签: android android-fragments

我的Fragment1 xml布局有:

  • 默认情况下未选中的两个RadioButtons
  • 两个EditTexts(一个具有inputType ="十进制"另一个具有inputType ="数字"),具有可见性="已消失"
  • 一个按钮,同时具有可见性="已消失"

所以,最初他们看起来像这样:

Screenshot 1 - Two radio buttons, none selected.

然后点击任一单选按钮,所需的EditText和按钮可见,我可以输入值。

Screenshot 2 - Radio button clicked. One EditText and the button is now visible

单击按钮后,Fragment1将替换为Fragment2。 然后在按下后退键时,后面的堆栈不会恢复EditText和Button的可见性信息。它显示所选的单选按钮,只有在单击它之后,我才能看到带有先前输入值的EditText和按钮。

enter image description here

我希望它显示输入的值和按钮,而不必单击电台选项。

My Fragment java:

public class Fragment1 extends Fragment {

// Member variables to store the visibility info of the two EditTexts and the button
int visibilityEditTextArea;
int visibilityEditTextNum;
int visibilityBtnEnterStateRec;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    ... 

    // OnClickListener for the first radio button, "Area of Farm".
    // Sets the visibility of the EditText with inputType="decimal" and the button to be VISIBLE, 
    // and intializes the member variables with the new visibility info.
    RadioButton radioButtonArea = (RadioButton) view.findViewById(R.id.radio_area_of_farm_to_fertilize);
    radioButtonArea.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Activity activity = getActivity();

            EditText editTextArea = (EditText) activity.findViewById(R.id.editText_area);
            if (editTextArea.getVisibility() == View.GONE) {
                editTextArea.setVisibility(View.VISIBLE);
                visibilityEditTextArea = editTextArea.getVisibility();
            }

            EditText editTextNum = (EditText) activity.findViewById(R.id.editText_number);
            if (editTextNum.getVisibility() == View.VISIBLE) {
                editTextNum.setVisibility(View.GONE);
                visibilityEditTextNum = editTextArea.getVisibility();
            }

            Button btnEnterStateRec = (Button) activity.findViewById(R.id.btn_enter_state_rec);
            if (btnEnterStateRec.getVisibility() == View.GONE) {
                btnEnterStateRec.setVisibility(View.VISIBLE);
                visibilityBtnEnterStateRec = btnEnterStateRec.getVisibility();
            }
        }
    });

    // Similarly for the second radio button.
    RadioButton radioButtonNum = (RadioButton) view.findViewById(R.id.radio_number_of_plants);
    radioButtonNum.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Activity activity = getActivity();

            EditText editTextArea = (EditText) activity.findViewById(R.id.editText_area);
            if (editTextArea.getVisibility() == View.VISIBLE) {
                editTextArea.setVisibility(View.GONE);
                visibilityEditTextArea = editTextArea.getVisibility();
            }

            EditText editTextNum = (EditText) activity.findViewById(R.id.editText_number);
            if (editTextNum.getVisibility() == View.GONE) {
                editTextNum.setVisibility(View.VISIBLE);
                visibilityEditTextNum = editTextArea.getVisibility();
            }

            Button btnEnterStateRec = (Button) activity.findViewById(R.id.btn_enter_state_rec);
            if (btnEnterStateRec.getVisibility() == View.GONE) {
                btnEnterStateRec.setVisibility(View.VISIBLE);
                visibilityBtnEnterStateRec = btnEnterStateRec.getVisibility();
            }
        }
    });

    // OnClickListener for the Button, "ENTER STATE RECOMMENDATIONS".
    // Puts the member variables in a bundle, and then replaces the container with Fragment2
    Button buttonEnterStateRec = (Button) view.findViewById(R.id.btn_enter_state_rec);
    buttonEnterStateRec.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // Puts the member variables in a bundle before making fragment transaction
            Bundle outState = new Bundle();
            outState.putInt("visibility_eT_area", visibilityEditTextArea);
            outState.putInt("visibility_eT_num", visibilityEditTextNum);
            outState.putInt("visibility_btn_enterStateRec", visibilityBtnEnterStateRec);
            Fragment1 fragment1 = new Fragment1();
            fragment1.setArguments(outState);

            Fragment2 fragment2 = new Fragment2();
            FragmentTransaction transaction = getFragmentManager().beginTransaction();
            transaction.replace(R.id.fragment_container, fragment2);
            transaction.addToBackStack(null);
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            transaction.commit();
        }
    });


    // Restores the member variables with the arguments from the bundle
    Bundle args = this.getArguments();
    if (args != null) {
        visibilityEditTextArea = args.getInt("visibility_eT_area");
        visibilityEditTextNum = args.getInt("visibility_eT_num");
        visibilityBtnEnterStateRec = args.getInt("visibility_btn_enterStateRec");

        // Checks if the int value of the member variable equals to that of View.VISIBLE.
        // If it does then sets the respective View to be visible.
        if (visibilityEditTextArea == View.VISIBLE) {
            view.findViewById(R.id.editText_area).setVisibility(View.VISIBLE);
        }
        if (visibilityEditTextNum == View.VISIBLE) {
            view.findViewById(R.id.editText_number).setVisibility(View.VISIBLE);
        }
        if (visibilityBtnEnterStateRec == View.VISIBLE) {
            view.findViewById(R.id.btn_enter_state_rec).setVisibility(View.VISIBLE);
        }
    }

    return view;
}

// Overides onPause() to save the visibility arguments in a bundle.
@Override
public void onPause() {
    super.onPause();

    Bundle outState = new Bundle();
    outState.putInt("visibility_eT_area", visibilityEditTextArea);
    outState.putInt("visibility_eT_num", visibilityEditTextNum);
    outState.putInt("visibility_btn_enterStateRec", visibilityBtnEnterStateRec);
    Fragment1 fragment1 = new Fragment1();
    fragment1.setArguments(outState);
}

}

XML就像这样。我已删除了ID和布局对齐详细信息。:

<RelativeLayout
    android:focusable="true"
    android:focusableInTouchMode="true">

    ...
    <RadioGroup>

        <RadioButton
            android:text="Area of farm to fertilize" />

        <RadioButton
            android:text="Number of plants or trees" />

    </RadioGroup>

    <android.support.design.widget.TextInputLayout>

        <EditText
            android:hint="In hectare"
            android:inputType="numberDecimal"
            android:visibility="gone"
            tools:visibility="visible" />

    </android.support.design.widget.TextInputLayout>

    <android.support.design.widget.TextInputLayout>

        <EditText
            android:inputType="number"
            android:visibility="gone"
            tools:visibility="visible" />

    </android.support.design.widget.TextInputLayout>

    <Button
        android:text="Enter state recommendations"
        android:visibility="gone"
        tools:visibility="visible" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

实际上,FragmentActivity(或AppCompatActivity)的当前实现会为您恢复查看状态的肮脏工作,您所要做的就是为您的Fragment提供ID或者标签

因此,在添加第一个片段(或您希望受自动视图状态保留的任何片段)时,请执行以下操作:

// probably in onCreate() of your activity:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Note the 3rd parameter -- it's the tag
transaction.add(R.id.fragment_container, fragment1, "FRAGMENT1");

现在,当您从后台堆栈中弹出frag2-frag1事务时,它将被恢复。

onActivityCreated()你可以这样:

public View onActivityCreated(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    ...
    if (savedInstanceState != null) {
        // pick up stuff from savedInstanceState
        // this is for the cases when onSaveInstanceState() has been called
        // i.e. for when activity's onSaveInstanceState() has been called
        // not for back stack events
    }

onPause() / onCreateView()捆绑操作移至onSaveInstanceState()onActivityCreated() - 所有其他情况将由FragmentManager自动处理。

答案 1 :(得分:0)

通过在单选按钮上实现 setOnCheckedChangeListener 来解决此问题。 OnClickListener没有提供Compound Buttons所需的行为,例如复选框,单选按钮,开关和切换按钮。