我有一个ListFragment
,我希望使用onSaveInstanceState
保存其数据并在onCreateView
中恢复,但会发生一些奇怪的事情。
我使用此代码保存数据:
private ArrayList<TuplaFacturaWS> 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<TuplaCupsWS> arrayCups = myApplication.getUsuarioActual().getCups();
savedInstanceState.putInt("numCups", arrayCups.size());
for(int cups = 0; cups < 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<TuplaFacturaWS> arrayFacturas = arrayCups.get(cups).getFacturas();
savedInstanceState.putInt("numFacturas"+cups, arrayFacturas.size());
for(int factura = 0; factura < 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());
}
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());
//...
}
会发生什么:
onCreateView
在创建视图上打印“num facturas:5”。onSaveInstanceState
在save:5上打印“N facturas”,onCreateView
在create view:5“上打印”num facturas“。 - &GT;这是对的。onSaveInstanceState
在save:0上打印“N facturas”,onCreateView
在create view:0上打印“num facturas”。 - &GT;这是错误的。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<TuplaCupsWS> arrayCups = new ArrayList<TuplaCupsWS>();
int numCups = savedInstanceState.getInt("numCups");
for(int cups = 0; cups < 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<TuplaFacturaWS> arrayFacturas = new ArrayList<TuplaFacturaWS>();
int numFacturas = savedInstanceState.getInt("numFacturas"+cups);
System.out.println("n facturas: "+numFacturas);
for(int factura = 0; factura < 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());
//...
}
在这种情况下会发生什么:
onCreateView
在创建视图上打印“num facturas:5”。onSaveInstanceState
在保存时打印“N facturas:5”,Bundle
不为空,onCreateView
打印“n facturas:0”和“创建视图上的num facturas:0”。 - &GT;这是错误的。如果我添加
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
与情况1相同。
我无法找到问题所在。
答案 0 :(得分:0)
我看到你在onCreateView()中调用了这个int numFacturas = savedInstanceState.getInt("numFacturas");
,但我没有看到你在onSaveInstanceState()中设置该值的位置。
这会导致numFacturas = 0,因此永远不会进入你的循环来填充数组。