我有一个有viewpager的活动,这个viewpager包含Fragment A和Fragment B. 片段B还有一个viewpager,它包含例如一个C片段。 Fragment C实现了Fragment B的内部接口OnNextButtonListener。
FragmentB.java:
public class FragmentB extends Fragment{
private OnNextButtonListener mFragmentCListener;
private Button mNextButton;
//instance variables
//other methods
@Override
View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_b, container, false);
init(view);//initialize views
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//mFragmentCListener is instance of FragmentC casted to OnNextButtonListener
mFragmentCListener.onNextButtonClick();
});
return view;
}
public interface OnNextButtonListener{
void onNextButtonClick();
}
}
FragmentC.java:
public class FragmentC extends Fragment implements OnNextButtonListener{
//...
private EditText mEditText;
@Override
View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_c, container, false);
init(view);//initialized mEditText and it is not null on rotation
return view;
}
@Override
void onNextButtonClick(){
String text = (String)mEditText.getText();//mEditText gets null on rotation
//...
}
}
一切正常但是当旋转设备然后单击mNextButton(片段B)时,onNextButtonClick中的mEditText(片段C)变为null但在onCreateView中仍然不为null。 我在onNextButtonClick中调试了第一行,调试器显示了debugger.png