Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 调用onDestroy回调时如何保存计数器的值?_Android_Ondestroy - Fatal编程技术网

Android 调用onDestroy回调时如何保存计数器的值?

Android 调用onDestroy回调时如何保存计数器的值?,android,ondestroy,Android,Ondestroy,我正在开发一个小而简单的android应用程序,在这个程序中,我可以通过点击按钮来增加或减少计数器。 当应用程序运行时,我将当前计数器值保存在文本文件中,当我返回应用程序时,我将读取写入的值并将其显示在文本视图中的屏幕上。问题是,当我回到应用程序并收到值并尝试增加它时,它开始从0开始计数。但是,如果我离开应用程序并调用onDestroy回调,则会出现问题,但如果我旋转手机并调用相同的onDestroy,计数器将按预期工作。我的问题是如何从最新保存的值继续增加计数器 这是我的密码: 主要活动 pu

我正在开发一个小而简单的android应用程序,在这个程序中,我可以通过点击按钮来增加或减少计数器。 当应用程序运行时,我将当前计数器值保存在文本文件中,当我返回应用程序时,我将读取写入的值并将其显示在
文本视图中的屏幕上。问题是,当我回到应用程序并收到值并尝试增加它时,它开始从0开始计数。但是,如果我离开应用程序并调用
onDestroy
回调,则会出现问题,但如果我旋转手机并调用相同的
onDestroy
,计数器将按预期工作。我的问题是如何从最新保存的值继续增加计数器

这是我的密码:

主要活动

public class MainActivity extends AppCompatActivity {
    private static final String FILE_NAME = "test.txt";
    TextView textView;
    private int count;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.text1);

        readTextFile();


        if (savedInstanceState != null){

            count = savedInstanceState.getInt("count");
            textView.setText(String.valueOf(count));

        }
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);

        outState.putInt("count", count);

    }

    public void decrement(View view) {

        count --;
        textView.setText(String.valueOf(count));
    }

    public void increment(View view) {
        count++;
        textView.setText(String.valueOf(count));
    }



    @Override
    protected void onStop() {
        super.onStop();

        String text = String.valueOf(count);
        FileOutputStream fos = null;

        try {
            fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
            try {
                fos.write(text.getBytes());
                Toast.makeText(this, "Saved to " + getFilesDir() + FILE_NAME, Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        count = Integer.parseInt(text);

        Log.i(TAG,"onStop");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG,"onPause");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG,"onDestroy");
    }

    public void readTextFile(){

        FileInputStream fis = null;

        try {
            fis = openFileInput(FILE_NAME);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            String text;

            while ((text = br.readLine()) != null){
                sb.append(text);
            }
            textView.setText(sb.toString());


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }

}
布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:screenOrientation="sensorLandscape"


    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text1"
        android:layout_width="167dp"
        android:layout_height="45dp"
        android:gravity="center"
        android:text="0"
        android:textColor="#0C0C0C"
        android:textSize="36sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.603" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="decrement"
        android:text="-"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/text1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="increment"
        android:text="+"
        app:layout_constraintBottom_toTopOf="@+id/text1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

您没有在
readTextFile()
中设置
count
的值,所以它是0。当您旋转手机时,它可以工作,因为您在
savedInstanceState

好的地方:)保存了值,现在它可以工作了。谢谢:)