-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
125 lines (110 loc) · 3.93 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.israilbony.notepad;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends Activity {
TextView readingView;
TextView appRestartsView;
private int howManyTimesBeenRun = 0;
private static final String NUMBER_OF_TIMES_RUN_KEY = "NUMBER";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_persistence);
readingView = (TextView) findViewById(R.id.displayView);
appRestartsView = (TextView) findViewById(R.id.applicationRestarts);
SharedPreferences sharedPreferences=getPreferences(Context.MODE_PRIVATE);
int defaultValue = 0;
howManyTimesBeenRun = sharedPreferences.getInt(NUMBER_OF_TIMES_RUN_KEY,0);
if (howManyTimesBeenRun == 0)
{
Toast.makeText(this, "Welcome to your new magic notepad!", Toast.LENGTH_LONG).show();
}
howManyTimesBeenRun++;
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(NUMBER_OF_TIMES_RUN_KEY,howManyTimesBeenRun);
editor.commit();
appRestartsView.setText(String.valueOf(howManyTimesBeenRun));
}
@Override
protected void onResume() {
super.onResume();
readingView.setText(getTextFile());
}
@Override
protected void onPause() {
saveTextFile(readingView.getText().toString());
super.onPause();
}
//Reading and Writing
private static final String DATA_FILE = "my_file";
public String getTextFile() {
FileInputStream fileInputStream = null;
String fileData = null;
try
{
fileInputStream = openFileInput(DATA_FILE);
int size = fileInputStream.available();
byte[] buffer = new byte[size];
fileInputStream.read(buffer);
fileInputStream.close();
fileData = new String(buffer, "UTF-8");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return fileData;
}
public void saveTextFile(String content) {
FileOutputStream fileOutputStream = null;
try
{
fileOutputStream = openFileOutput(DATA_FILE,Context.MODE_PRIVATE);
fileOutputStream.write(content.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
try
{
if
(fileOutputStream != null)
{
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.persistence, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}