SantiagoGlz
SantiagoGlz

Reputation: 17

save data on screen rotation using fragments

I'm working on an application in android studio. Use a different XML file to manage the landscape mode. This is in the "layout-land" folder

The problem is that when you change portrait to landscape the values entered in the EditText are lost

How I can prevent this from happening?

I use the Navigation Drawer Activity from Android Studio so this is the code where I show the fragment:

 public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camara) {
      fragmentCalcularIMC fragment = new fragmentCalcularIMC();
        android.support.v4.app.FragmentTransaction fragmentTransaction =
                getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container,fragment);
        fragmentTransaction.commit();

    } 

And here is the code that I have in the java file of the fragment:

public class fragmentCalcularIMC extends Fragment  {


View v;
private String varaltura;
private EditText etaltura, etpeso, etedad;
public fragmentCalcularIMC() {

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

     v = inflater.inflate(R.layout.fragment_fragment_calcular_imc, container, false);


    etaltura = (EditText)v.findViewById(R.id.editTexth);
    etpeso = (EditText)v.findViewById(R.id.editText2);
    String [] values =  getResources().getStringArray(R.array.valoresEstatura);
    Spinner spinner = (Spinner) v.findViewById(R.id.spinner);
    EditText editText1 = (EditText) v.findViewById(R.id.editTexth);


    ArrayAdapter<String> LTRadapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values);
    LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    spinner.setAdapter(LTRadapter);

    spinner.setAdapter(LTRadapter);


    // Inflate the layout for this fragment
    return v;

}}

Upvotes: 0

Views: 973

Answers (2)

Simon
Simon

Reputation: 1731

You can try to set setRetainInstance() like this in the onCreate of the fragment.

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRetainInstance(true);

    }

Upvotes: -1

Zeeshan Shabbir
Zeeshan Shabbir

Reputation: 7114

You need to override onSaveInstanceState() method for this purpose.

Save your data like this

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("data", yourdata);  
 }

then retain it in onCreateView()

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if(savedInstanceState!=null)
    {
        int a=savedInstanceState.getInt("data");
        //handle your data on screen rotation
    }

//the rest of your code then....
}

Hope it helps

Upvotes: 3

Related Questions