Data persistence on Android

2

Goodbye, I'm creating an app where in the first activity I filter the regions of the country and then I pass them through the following activity where I call a webservice with the respective filter to bring me the correct data, after that clicking on the filtered list opens another activity with the detail of the item clicked, but when I return to the list it tells me that the putextra is empty, the data that came from the filter of the first activity is lost, I hope to be clear.

First activity, I send the filter

if (position == 0) {
                Intent intent = new Intent(Turismo.this,Contrato.class);
                intent.putExtra("region",spinnerRegion.getSelectedItemPosition());
                intent.putExtra("grilla",position);
                startActivity(intent);

In the second I receive it and later I call the web service, until there all good. I filled a recyclerview for the view.

 Intent intent = getIntent();
    Bundle extra = intent.getExtras();
    datoregion = (String)extra.get("region").toString();
    String datgrilla = (String)extra.get("grilla").toString();

in the third activity I show the detail that brings the web service.

nombre.setText(getIntent().getStringExtra("nombre"));
    descripcion.setText(getIntent().getStringExtra("descripcion"));
    Picasso.with(context).load(getIntent().getStringExtra("imagen_public")).into(imageView);

When I come back from the third activity I get the error

  

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get (java.lang.String)' on a null object reference

I've found out about sharepreferences but I do not know where to put it. Thank you very much in advance.

    
asked by Aldo Gallardo 15.07.2016 в 17:04
source

2 answers

1

Save the bundle values using the onSaveInstanceState () method:

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
 super.onSaveInstanceState(savedInstanceState);
      // El bundle sera guardado y enviado al onCreate() de la Activity.
      savedInstanceState.putString("region", valorRegion);
      savedInstanceState.putString("grilla", valorGrilla);
    }

In this way you can get them back in the activity, through onRestoreInstanceState (), you can recover the values:

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  // Restore UI state from the savedInstanceState.

  String valorRegion = savedInstanceState.getString("region");

String valorGrilla = savedInstanceState.getString("grilla");

} 
    
answered by 15.07.2016 в 20:51
1

You can use sharedPreferences to store the data:

//de esta manera obtienes los datos del SharedPreferences.
SharedPreferences datos = getSharedPreferences("datos", 0);
String datoregion = datos.getString("region","null");
String datgrilla = datos.getString("grilla","null");

//de esta manera insertas los datos
SharedPreferences.Editor editor = datos.edit();
editor.putString("region", valorRegion);
editor.putString("grilla", valorGrilla);
editor.commit();

The sharedpreferences would be like a text document that is responsible for keeping data in the phone's memory.

    
answered by 17.07.2016 в 21:43