调用 onDestroy 回调时如何保存计数器的值?
How to save value of counter when onDestroy callback is called?
我正在开发一个小而简单的 android 应用程序,我可以在其中增加或减少点击按钮时的计数器。
当应用程序运行 onStop
时,我将当前计数器值保存在文本文件中,当我返回应用程序时,我正在读取写入的值并将其显示在 TextView
中的屏幕上.问题是,当我回到应用程序并接收值并尝试递增它时,它从 0 开始计数。但是,如果我离开应用程序并调用 onDestroy
回调,就会出现问题,但是如果我旋转 phone 并且相同的 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。当您旋转 phone 时它起作用,因为您在 savedInstanceState
保存了值。
我正在开发一个小而简单的 android 应用程序,我可以在其中增加或减少点击按钮时的计数器。
当应用程序运行 onStop
时,我将当前计数器值保存在文本文件中,当我返回应用程序时,我正在读取写入的值并将其显示在 TextView
中的屏幕上.问题是,当我回到应用程序并接收值并尝试递增它时,它从 0 开始计数。但是,如果我离开应用程序并调用 onDestroy
回调,就会出现问题,但是如果我旋转 phone 并且相同的 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。当您旋转 phone 时它起作用,因为您在 savedInstanceState
保存了值。