0

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);
}
Aalap Patel
  • 2,058
  • 2
  • 17
  • 31

2 Answers2

1

Instead of

fragmentTransaction.replace(R.id.fragment_container, secondFragment);

Use

fragmentTransaction.add(R.id.fragment_container, secondFragment);

Edit

If this isn't working then you will have to use addToBackstack in your transaction, that will surely work. However you will also need to override onBackPressed function in Main activity and popbackStack to pop the fragment before returning to the previous one. This is how you need to override it.

Community
  • 1
  • 1
varunkr
  • 5,364
  • 11
  • 50
  • 99
0

It looks like you're overwriting the savedInstance data you're getting inside your onCreate with the values you're setting in onCreateView. Try commenting out the code in your onCreate and replacing the top lines inside of your onCreateView with:

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);
next1 = (TextView) rootView.findViewById(R.id.next1);

if(savedInstanceState != null){ //the data's on the savedinstance
   firstName = savedInstanceState.getString("firstname");
   lastName = savedInstanceState.getString("lastname");
   //restore the editTexts
   fname.setText(firstName);
   lname.setText(lastName);
}else{
   firstName = fname.getText().toString();
   lastName = lname.getText().toString();

   //whatever you want to do on the first run
   //or if the data's not on the savedInstance
}

//whatever else you want to do on every run
Sammy T
  • 1,924
  • 1
  • 13
  • 20