私はサインアッププロセスのために5つのフラグメントを配置しています。最初のフラグメントには2つの編集テキストと1つのテキストビューがあります。編集テキストは姓と名を表し、テキストビューは次のフラグメントに移動します2。私がfrag2からfrag1に戻ると、そのfirstnameとlastnameの値は消えます。 onSavedInstanceメソッドにputStringメソッドを使用して値を格納しようとしましたが、onCreateメソッドで値を取得しましたが、それは役に立ちませんでした。代わりに戻り中にフラグメントのEditTextsに値を保持する
fragmentTransaction.replace(R.id.fragment_container, secondFragment);
のここでのコードがある...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState==null){
}
else{
firstName=savedInstanceState.getString("firstname");
lastName=savedInstanceState.getString("lastname");
fname.setText(firstName);
lname.setText(lastName);
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag1,container,false);
fname = (EditText) rootView.findViewById(R.id.si_firstname);
lname = (EditText) rootView.findViewById(R.id.si_lastname);
firstName = fname.getText().toString();
lastName = lname.getText().toString();
next1 = (TextView) rootView.findViewById(R.id.next1);
next1.setOnClickListener(new View.OnClickListener() { //takes to frag2.
@Override
public void onClick(View v) {
if(firstName.isEmpty() || lastName.isEmpty()){
Toast.makeText(getActivity().getApplicationContext(), "no name", Toast.LENGTH_LONG).show();
}
else {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, secondFragment);
fragmentTransaction.commit();
}
}
});
return rootView;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("firstname", firstName);
outState.putString("lastname", lastName);
}
:とあなたの
onCreate
内のコードをコメントアウトして、onCreateView
の内側にトップラインを交換してみてください? – varunkrフレグ2の戻るボタンです。 –
あなたは私の答えを試しましたか? – varunkr