I have an activity where I have put 5 fragments for signup process. First fragment has two edit texts and a textview. Edit texts are for first and last name and text view takes me to next fragment2. Whenn I come back from frag2 to frag 1, those firstname and lastname values disappears. I have tried storing values in onSavedInstance method with putString method and retrieve it in onCreate method but its not helping. Here is the code...
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);
}