2015-08-23 51 views
0

我的片段1 XML佈局恢復有:查看知名度是沒有得到從堆棧中

  • 兩個單選按鈕,默認情況下選中
  • 兩個EditTexts(一用的inputType =「十進制」和其他與的inputType =「號「),具有可識別性=」 水漲船高」
  • 按鈕,也能見度= 「水漲船高」

所以,起初他們似乎是這樣的:

Screenshot 1 - Two radio buttons, none selected.

然後在單擊任一個單選按鈕時,所需的EditText和按鈕將變爲可見,並且我可以輸入值。

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

在點擊按鈕,片段1被替換爲Fragment2。 然後在按下後退鍵時,後退堆棧不會恢復EditText和Button的可見性信息。它顯示所選的單選按鈕,並且只有在單擊它之後,我才能看到帶有先前輸入值的EditText和按鈕。

enter image description here

我希望它顯示與按鈕一起輸入的數值,而無需點擊單選選項。

我的片段的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是這樣的。我已經刪除的識別碼與佈局對齊的詳細資料。:

<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> 

回答

0

通過在單選按鈕上實現setOnCheckedChangeListener來修復它。 OnClickListener不會像複選框,單選按鈕,開關和切換按鈕那樣提供Compound Buttons所需的行爲。

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自動處理。

+0

感謝您提及標籤參數。它從我的add()中丟失。 (更多關於片段標籤在這裏的重要性的信息:http://stackoverflow.com/a/13510619/1944049)儘管如此,它仍然不能恢復可見性狀態。我想我必須改變我的設計。 –