title: Save and restore data in a Fragment
body: <p>I have a <code>ListFragment</code> where I want to save its data using <code>onSaveInstanceState</code>and restore it in <code>onCreateView</code>, but something strange happens.</p>
  
  <p><strong>I use this code for saving data:</strong></p>
  
  <pre><code>private ArrayList&lt;TuplaFacturaWS&gt; facturas;
  
  @Override
  public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);
      System.out.println("save");
  
      //TuplaUsuario.usuario
      savedInstanceState.putString("usuario", myApplication.getUsuarioActual().getUsuario());
      //TuplaUsuario.password
      savedInstanceState.putString("password", myApplication.getUsuarioActual().getPassword());
      //TuplaUsuario.cups
      ArrayList&lt;TuplaCupsWS&gt; arrayCups = myApplication.getUsuarioActual().getCups();
      savedInstanceState.putInt("numCups", arrayCups.size());
      for(int cups = 0; cups &lt; arrayCups.size(); cups++) {
          //TuplaUsuario.cups.CUPS, TuplaUsuario.cups.Tarifa, TuplaUsuario.cups.Direccion
          savedInstanceState.putString("cups"+cups, arrayCups.get(cups).getCups());
          savedInstanceState.putString("tarifa"+cups, arrayCups.get(cups).getTarifa());
          savedInstanceState.putString("direccion"+cups, arrayCups.get(cups).getDireccion());
          //TuplaUsuario.cups.facturas
          System.out.println("N facturas on save: "+arrayCups.get(cups).getFacturas().size());
          ArrayList&lt;TuplaFacturaWS&gt; arrayFacturas = arrayCups.get(cups).getFacturas();
          savedInstanceState.putInt("numFacturas"+cups, arrayFacturas.size());
          for(int factura = 0; factura &lt; arrayFacturas.size(); factura++) {
              savedInstanceState.putString("factura"+factura,
                      arrayFacturas.get(factura).getFactura());
              savedInstanceState.putDouble("importe"+factura, 
                      arrayFacturas.get(factura).getImporte());
              savedInstanceState.putLong("fecha"+factura, 
                      arrayFacturas.get(factura).getFecha().getTime());
              savedInstanceState.putString("plan"+factura,    
                      arrayFacturas.get(factura).getPlan());
              savedInstanceState.putString("estado"+factura,  
                      arrayFacturas.get(factura).getEstado());
              savedInstanceState.putString("pdf"+factura, 
                      arrayFacturas.get(factura).getPdf());
              savedInstanceState.putString("anexo"+factura,   
                      arrayFacturas.get(factura).getAnexo());
          }
      }
      //TuplaUsuario.cupsActual
      savedInstanceState.putInt("cupsActual", myApplication.getUsuarioActual().getCupsActual());
  }
  </code></pre>
  
  <h2><strong>Situation 1</strong></h2>
  
  <pre><code>public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState)
  {
      View view = inflater.inflate(R.layout.activity_facturas, container, false);
      // Get the application instance
      myApplication = (MyApplication)getActivity().getApplication();
      facturas = myApplication.getFacturas();
  
      System.out.println("num facturas on create view: "+facturas.size());
  
      //...
  }
  </code></pre>
  
  <p>What happens is:</p>
  
  <ul>
  <li>When load view for first time, view shows 5 "facturas". <code>onCreateView</code>prints "num facturas on create view: 5".</li>
  <li>When change orientation, view shows same 5 "facturas". <code>onSaveInstanceState</code>prints "N facturas on save: 5", and <code>onCreateView</code>prints "num facturas on create view: 5". -> It is correct.</li>
  <li>When change orientation again, view shows no "factura". <code>onSaveInstanceState</code>prints "N facturas on save: 0", and <code>onCreateView</code>prints "num facturas on create view: 0". -> It is wrong.</li>
  </ul>
  
  <h2><strong>Situation 2</strong></h2>
  
  <pre><code>public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState)
  {
      View view = inflater.inflate(R.layout.activity_facturas, container, false);
      // Get the application instance
      myApplication = (MyApplication)getActivity().getApplication();
  
      if (savedInstanceState != null) {
              System.out.println("bundle not null");
              myApplication.nuevoUsuarioActual();
              //TuplaUsuario.usuario
              myApplication.getUsuarioActual().setUsuario(savedInstanceState.getString("usuario"));
              //TuplaUsuario.password
              myApplication.getUsuarioActual().setPassword(savedInstanceState.getString("password"));
              //TuplaUsuario.cups
              ArrayList&lt;TuplaCupsWS&gt; arrayCups = new ArrayList&lt;TuplaCupsWS&gt;();
              int numCups = savedInstanceState.getInt("numCups");
              for(int cups = 0; cups &lt; numCups; cups++) {
                  TuplaCupsWS tuplaCups = new TuplaCupsWS();
                  //TuplaUsuario.cups.CUPS, TuplaUsuario.cups.Tarifa, TuplaUsuario.cups.Direccion
                  tuplaCups.setCups(savedInstanceState.getString("cups"+cups));
                  tuplaCups.setTarifa(savedInstanceState.getString("tarifa"+cups));
                  tuplaCups.setDireccion(savedInstanceState.getString("direccion"+cups));
                  //TuplaUsuario.cups.facturas
                  ArrayList&lt;TuplaFacturaWS&gt; arrayFacturas = new ArrayList&lt;TuplaFacturaWS&gt;();
              int numFacturas = savedInstanceState.getInt("numFacturas"+cups);
              System.out.println("n facturas: "+numFacturas);
              for(int factura = 0; factura &lt; numFacturas; factura++) {
  
                  TuplaFacturaWS tuplaFactura = new TuplaFacturaWS();
                  tuplaFactura.setFactura(savedInstanceState.getString("factura"+factura));
                  tuplaFactura.setImporte(savedInstanceState.getDouble("importe"+factura));
                  tuplaFactura.setFecha(new Date(savedInstanceState.getLong("fecha"+factura)));
                  tuplaFactura.setPlan(savedInstanceState.getString("plan"+factura));
                  tuplaFactura.setEstado(savedInstanceState.getString("estado"+factura));
                  tuplaFactura.setPdf(savedInstanceState.getString("pdf"+factura));
                  tuplaFactura.setAnexo(savedInstanceState.getString("anexo"+factura));
                  arrayFacturas.add(tuplaFactura);
  
              }
                  tuplaCups.setFacturas(arrayFacturas);
                  arrayCups.add(tuplaCups);
              }
              myApplication.getUsuarioActual().setCups(arrayCups);
              //TuplaUsuario.cupsActual
              myApplication.getUsuarioActual().setCupsActual(
                      savedInstanceState.getInt("cupsActual"));
  
      }
  
      facturas = myApplication.getFacturas();
  
      System.out.println("num facturas on create view: "+facturas.size());
  
      //...
  }
  </code></pre>
  
  <p>In that case what happens is:</p>
  
  <ul>
  <li>When load view for first time, view shows 5 "facturas". <code>onCreateView</code>prints "num facturas on create view: 5".</li>
  <li>When change orientation, view shows same 5 "facturas". <code>onSaveInstanceState</code>prints "N facturas on save: 5", <code>Bundle</code>is not null, and <code>onCreateView</code>prints "n facturas: 0" and "num facturas on create view: 0". -> It is wrong.</li>
  <li>When change orientation again, the same happens.</li>
  </ul>
  
  <p>If I add</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setRetainInstance(true);
  }
  </code></pre>
  
  <p>is the same as in Situation 1.</p>
  
  <p>I can't find where the problem is. </p>