Permalink
Browse files

Add saveData()

Signed-off-by: Simone <hello@simonewebdesign.it>
  • Loading branch information...
1 parent c996ae0 commit 86b4f998afc2fd13b9efc15ccbb14eb7f93cfb07 @simonewebdesign committed Mar 6, 2013
Showing with 16 additions and 18 deletions.
  1. +16 −18 src/com/example/todolist/MainActivity.java
@@ -98,40 +98,28 @@ public void onClick(View v) {
protected void onDestroy() {
super.onDestroy();
Log.v(TAG, "onDestroy called!");
-
- // Save data
- try {
- writeJSON();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
+ saveData();
}
@Override
protected void onPause() {
super.onPause();
Log.v(TAG, "onPause called!");
+ saveData();
}
@Override
protected void onResume() {
super.onResume();
Log.v(TAG, "onResume called!");
+ saveData();
}
@Override
protected void onStop() {
super.onStop();
Log.v(TAG, "onStop called!");
-
- // Save data
- try {
- writeJSON();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
+ saveData();
}
public String getJSON() throws IOException {
@@ -182,13 +170,12 @@ public String readJSON() throws FileNotFoundException, IOException {
fis.close();
byte[] buffer = new byte[1024];
- int length;
/* Reads a single byte from this stream and returns it as an integer
* in the range from 0 to 255. Returns -1 if the end of the stream
* has been reached. Blocks until one byte has been read, the end of
* the source stream is detected or an exception is thrown.
*/
- while ( (length = fis.read(buffer)) != -1 ) {
+ while ( (fis.read(buffer)) != -1 ) {
fileContent.append(new String(buffer));
}
@@ -223,4 +210,15 @@ public void restoreDataFromJSON(String json) {
e.printStackTrace();
}
}
+
+ public void saveData() {
+
+ try {
+ writeJSON();
+ } catch (IOException e) {
+
+ Log.e(TAG, "JSON writing failed.");
+ e.printStackTrace();
+ }
+ }
}

0 comments on commit 86b4f99

Please sign in to comment.