In my app the user goes to different activities to buy upgrades and I want it so that if they close the app on that activity their stuff will be saved.
This is my way of saving that information:
public void onStop(){
super.onStop();
writeCounttofile();
writeSingletofile();
writeAddtofile();
}
public void writeCounttofile() {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("countvalue.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(String.valueOf(countervalue));
outputStreamWriter.close();
} catch (IOException e) {
Log.v("MyActivity", e.toString());
}
}
private int readCountFromFile() {
String result = "";
int countervalue = 0;
try {
InputStream inputStream = openFileInput("countvalue.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String tempString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((tempString = bufferedReader.readLine()) != null) {
stringBuilder.append(tempString);
}
inputStream.close();
result = stringBuilder.toString();
countervalue = Integer.parseInt(result);
}
} catch (FileNotFoundException e) {
Log.v("MyActivity", "File not found" + e.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
//here you catch and watch the problem
Log.e("MyActivity", "cant parse string: " + result);
}
return countervalue;
}
public void writeAddtofile() {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("addvalue.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(String.valueOf(Add));
outputStreamWriter.close();
} catch (IOException e) {
Log.v("MyActivity", e.toString());
}
}
private int readAddFromFile() {
String result = "";
int add = 0;
try {
InputStream inputStream = openFileInput("addvalue.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String tempString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((tempString = bufferedReader.readLine()) != null) {
stringBuilder.append(tempString);
}
inputStream.close();
result = stringBuilder.toString();
add = Integer.parseInt(result);
}
} catch (FileNotFoundException e) {
Log.v("MyActivity", "File not found" + e.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
//here you catch and watch the problem
Log.e("MyActivity", "cant parse string: " + result);
}
return add;
}
public void writeSingletofile() {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("single.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(String.valueOf(Singleadd));
outputStreamWriter.close();
} catch (IOException e) {
Log.v("MyActivity", e.toString());
}
}
private int readSingleFromFile() {
String result = "";
int single = 1;
try {
InputStream inputStream = openFileInput("single.txt");
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String tempString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((tempString = bufferedReader.readLine()) != null) {
stringBuilder.append(tempString);
}
inputStream.close();
result = stringBuilder.toString();
single = Integer.parseInt(result);
}
} catch (FileNotFoundException e) {
Log.v("MyActivity", "File not found" + e.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
//here you catch and watch the problem
Log.e("MyActivity", "cant parse string: " + result);
}
return single;
}
And when they return to the app on the main activity I called all the read methods and have the values of the app equal those upgrades, but for some reason it doesn't work. When I close the app from the main activity it saves all the information fine, which is why I just copied the methods from the main activity to my other activities. Is this whats wrong?
Please and Thank You.