I have a logic in my application so that when OnStop() happens I save a file to the storage. However, if the file is large and I close application by swiping from Recents Tab my application is shut down before my file is saved. So it opens file then app dies and the file remains empty. Is that normal behaviour or the task killer on my phone is overplaying? Should I create a service to save a file certainly?
example code:
@Override
public void onStop() {
try (FileWriter writer = new FileWriter(path)) {
if (veryLargeFile == null) {
return;
}
String s = veryLargeFile.toString();
Log.d(TAG, "this message is shown");
writer.write(s);
Log.d(TAG, "this message is not shown");
writer.flush();
} catch (IOException ex) {}
super.onStop();
}
Example 2:
@Override
public void onPause() {
try (FileWriter writer = new FileWriter(path)) {
if (veryLargeFile == null) {
return;
}
String s = veryLargeFile.toString();
Log.d(TAG, "this message is shown");
writer.write(s);
Log.d(TAG, "this message is not shown");
writer.flush();
} catch (Exception ex) {
Log.d(TAG, "this message is not shown");
}
super.onPause();
}