每当我按下后退按钮时,我的应用程序都不会调用onSaveInstanceState()
,也无法保存数据。我正在尝试制作一个调度程序应用程序,即使按下后退按钮,我也需要保存已经设置的时间表。我想通过将源文件中的新数据添加到 stringArray 来动态编辑 ListView。我遇到的问题是文件 schedules.txt 没有保存。每当程序打开一个新活动时,该文件现在都是空白的。
这是我到目前为止的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_schedules);
ListView listview = (ListView) findViewById(R.id.scheduleListView);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, schedulesList);
listview.setAdapter(adapter);
Log.v("myapp", "currentlist of files associated with this program are: " + fileList());
try {
FileOutputStream fout = openFileOutput("schedules.txt", MODE_PRIVATE);
Log.v("myapp", "FileOutputStream ran");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
Log.v("myapp", "first get old schedules call");
getOldSchedules();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
public void editFile(String format) throws IOException {
Log.v("myapp", "editFile ran");
FileOutputStream fout = openFileOutput("schedules.txt", MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(fout);
writer.write("hello alex");
writer.flush();
writer.close();
Log.v("myapp", "secondary getoldschedules call");
getOldSchedules();
}
public void getOldSchedules() throws IOException{
FileInputStream fis = openFileInput("schedules.txt");
InputStreamReader reader = new InputStreamReader(fis);
char[] inputbuffer = new char[32];
reader.read(inputbuffer);
String data = new String(inputbuffer);
Log.v("myapp", "data in file reads: " + data);
reader.close();
}