我的应用程序有两个选项卡并在选项卡之间切换,这让我丢失了我在当前行程中输入的任何内容所以我修改了我的代码以保存状态。
package com.test.testing;
public class Trip2 extends Fragment {
EditText nameOfInf;
@Override
public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
if ((savedInstanceState != null) && (savedInstanceState.getString("nameof") != null)) {
Toast.makeText(getActivity(), savedInstanceState.getString("nameof"),2000).show();
}
else {
Toast.makeText(getActivity(), "DIDNT SAVE",2000).show();
}
final RelativeLayout mFrame2 = (RelativeLayout) inflater.inflate( R.layout.trip, container, false );
final Button btnCalc = (Button) mFrame2.findViewById(R.id.btnCalculate);
nameOfInf = (EditText) mFrame2.findViewById(R.id.etName);
final EditText tollAmount = (EditText) mFrame2.findViewById(R.id.etToll);
btnCalc.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//DO SOMETHING
}
});
return mFrame2;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("nameof", nameOfInf.toString());
}
}
它总是显示
没有保存
我该如何解决这个问题?