当活动停止时,OutputStreamWriter不保存数据

时间:2017-08-20 20:12:39

标签: android save local-storage storage outputstream

在我的应用程序中,用户进行不同的活动以购买升级,我希望它能够在他们关闭该活动的应用程序时保存它们的内容。

这是我保存这些信息的方式:

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;
}

当他们返回主活动的应用程序时,我调用了所有读取方法并使应用程序的值等于这些升级,但由于某种原因它不起作用。当我从主活动中关闭应用程序时,它会保存所有信息,这就是为什么我只是将方法从主活动复制到我的其他活动。这有什么不对吗?

请和谢谢。

1 个答案:

答案 0 :(得分:0)

根据我的经验,onStop方法不允许你执行某些操作,所以要解决它从onPause方法调用那些方法