我正在尝试做什么 ::我正在尝试将输入的数据保存到edittext和spinner onorientation更改
正在发生什么 :::我无法保存数据,我该如何解决此问题
MainActivity.java
public class MainActivity extends FragmentActivity {
Fragment_A frgObj;
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
Log.d("MAIN-ACTIVITY", "onCreate");
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Log.d("MAIN-ACTIVITY", "onStart");
frgObj=Fragment_A.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.container, frgObj).addToBackStack(null).commit();
}
public void onSaveInstanceState(Bundle outState){
getSupportFragmentManager().putFragment(outState,"myfragment",frgObj);
}
public void onRetoreInstanceState(Bundle inState){
frgObj = (Fragment_A) getSupportFragmentManager().getFragment(inState,"myfragment");
}
}
Fragment_A.java
public class Fragment_A extends Fragment{
Button btn;
Spinner spinner;
EditText editText;
SeekBar seekBar;
public static Fragment_A newInstance() {
Log.d("FRAGMENT-A", "newInstance");
Fragment_A fragment = new Fragment_A();
return fragment;
}
@Override
public void onAttach(Activity activity) {
Log.d("FRAGMENT-A", "onAttach");
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("FRAGMENT-A", "onCreate");
super.onCreate(savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
Log.d("FRAGMENT-A", "onSaveInstanceState");
super.onSaveInstanceState(outState);
//City Spinner state
outState.putString("editText", editText.getText().toString());
outState.putInt("yourSpinner", spinner.getSelectedItemPosition());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("FRAGMENT-A", "onCreateView");
View view=inflater.inflate(R.layout.fragment_a, container, false);
btn=(Button) view.findViewById(R.id.buttonId1);
spinner=(Spinner) view.findViewById(R.id.spinnerId1);
editText=(EditText) view.findViewById(R.id.editTextId1);
seekBar=(SeekBar) view.findViewById(R.id.seekBarId1);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d("FRAGMENT-A", "onActivityCreated");
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
//City Spinner state
editText.setText(savedInstanceState.getString("editText"));
spinner.setSelection(savedInstanceState.getInt("yourSpinner", 0));
}
}
@Override
public void onStart() {
Log.d("FRAGMENT-A", "onStart");
super.onStart();
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("FRAGMENT-A", "button Clicked");
Fragment_B frgObj=Fragment_B.newInstance();
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container, frgObj,"Fragment_B").addToBackStack(null).commit();
}
});
}
}
答案 0 :(得分:0)
除了致电super.onSaveInstanceState(Bundle)
之外,我认为您不需要更改活动中的任何内容。将数据onSaveInstanceState
保存在片段中就足够了。我会将恢复逻辑放在onCreateView
而不是onActivityCreated
中。保证在重新创建片段时调用onCreateView
,但onActivityCreated
不是这种情况。
您可以尝试的另一件事是不会使用setRetainInstance(true)
,而{{3}}应该通过方向更改来保留您的实例变量。