我想在我的应用程序是onStop时将对象打印到文件,并在onCreate上读取它。当我关闭应用程序并重新打开它时,对象不在那里。通过应用程序导航时,对象可以正常运行,仅在重新启动时出现问题。
该对象是TList类的实例(“tl”) - extends ArrayList
读:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
FileInputStream fis;
try {
fis = openFileInput("data_t");
ObjectInputStream ois = new ObjectInputStream(fis);
tl = (TList)ois.readObject();
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
写:
@Override
protected void onStop() {
super.onStop();
FileOutputStream fos;
try {
fos = openFileOutput("data_t", Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(tl);
oos.flush();
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}