eloy lopez
eloy lopez

Reputation: 11

Persistence of data from different layouts android

I was doing an android application, in which I have two different layouts for the same activity (one for portrait and one for landscape, it is important to say that they are totally different). Well my problem the persistence of data between them, since when changing between portrait and landscape the data is lost, to try to solve my problem I used onSaveInstanceState and change the manifest, but none of them serves in my case. I hope you can help me, regards.

pd. Landscape is in the directory layout-land

mensaje=(EditText) findViewById(R.id.EditTextAlarma); //portrait
horaEdit=(EditText) findViewById(R.id.editTextHoras); //landscape
minutosEdit=(EditText) findViewById(R.id.editTextMinutos); //landscape
segundosEdit=(EditText) findViewById(R.id.editTextSegundos); //landscape
mensajeTimer=(EditText) findViewById(R.id.mensajeTimer); //landscape

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("msj",mensaje.getText().toString());
    outState.putString("hora",horaEdit.getText().toString());
    outState.putString("min",minutosEdit.getText().toString());
    outState.putString("seg",segundosEdit.getText().toString());
    outState.putString("msjT",mensajeTimer.getText().toString());

}


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    String m=savedInstanceState.getString("msj");
    String h=savedInstanceState.getString("hora");
    String mn=savedInstanceState.getString("min");
    String s=savedInstanceState.getString("seg");
    String mt=savedInstanceState.getString("msjT");
    mensaje.setText(m);
    horaEdit.setText(h);
    minutosEdit.setText(mn);
    segundosEdit.setText(s);
    mensajeTimer.setText(mt);

}

Upvotes: 0

Views: 80

Answers (1)

Pavan kumar
Pavan kumar

Reputation: 61

Override this two methods from your activity

Then when orientation changes just get the value in onRestoreInstanceState and set it to xml widget

 @Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //SAVE YOUR DATA HERE
    outState.putString("key","Value");
    //YOU CAN SAVE ANY TYPE OF DATA HERE AND RETRIVE

}


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    //RETRIVE YOUR DATA HERE
    String value=savedInstanceState.getString("key");
    //SET VALUE TO XML HERE
    Log.d("Value saved is:",value);
    
}

Upvotes: 2

Related Questions