我有一个Android应用,我有一个带有自定义适配器的 ListView ,有些项目是静态,有些是动态(从网址加载)。
它运作完美,没有错误,但是当我暂停我的应用程序并使用其他应用程序(比如使用chrome浏览)时,当我返回App时,静态项目就在那里,但是动态项目已消失。
编辑:我正在使用volley StringRequest和Gson从网址加载项目。
答案 0 :(得分:0)
您必须存储已获取的数据,您可以通过两种不同的方式实现此目的
使用onSaveInstanceState和onRestoreInstanceState方法返回应用程序时,可以保存该数据并将其还原
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// save your date like this
savedInstanceState.putBoolean("MyBoolean", true);
savedInstanceState.putDouble("myDouble", 1.9);
savedInstanceState.putInt("MyInt", 1);
savedInstanceState.putString("MyString", "Welcome back to Android");
// etc.
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore your date like this and assign it to your list or where ever you need
// This bundle has also been passed to onCreate.
boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
double myDouble = savedInstanceState.getDouble("myDouble");
int myInt = savedInstanceState.getInt("MyInt");
String myString = savedInstanceState.getString("MyString");
}
您可以将其存储在数据库中,并在移动到onRestoreSaveInstance时将其还原回来
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// fetch data from database and assign it back to your UI
}