gpt4 book ai didi

java - 共享首选项不保存 Android 应用程序的确切实例状态

转载 作者:行者123 更新时间:2023-12-01 07:34:24 26 4
gpt4 key购买 nike

我正在使用 Java 创建一个简单的点击计数器 Android 应用程序。我是 Java 新手。我试图在应用程序退出时保存计数,无论是否按下后退按钮、应用程序关闭或崩溃等。这是迄今为止我的代码:

public class wazeefa extends Activity  {

//Count Button
TextView txtCount;
Button btnCount;
int count;
Button wmute;
Button wreset;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wazeefa);

//SAVE COUNT
SharedPreferences app_preferences =
PreferenceManager.getDefaultSharedPreferences(this);

count = app_preferences.getInt("count", 0);

txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText("This app has been started " + count + " times.");

SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", ++count);
editor.commit();

//Button SOUND AND COUNT
final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.bubble);

txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.wclick);

btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
final ImageView image = (ImageView) findViewById(R.id.imageview);
count++;
if (count > 50) count = 0; image.setImageResource(R.drawable.duroodimage);
if (count > 0) image.setImageResource(R.drawable.duroodimage);
if (count > 9) image.setImageResource(R.drawable.zikrimage);
if (count > 39) image.setImageResource(R.drawable.duroodimage);
txtCount.setText(String.valueOf(count));
mpButtonClick.start();

//RESET Button
wreset = (Button)findViewById(R.id.wreset);
wreset.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
count = 0;
image.setImageResource(R.drawable.duroodimage);;
txtCount.setText("0");


}

我的应用程序遇到两个问题。

首先,当应用程序关闭然后再次打开时,计数不会保存到原来的状态。例如,如果计数为“20”并且我点击后退按钮,则返回到同一页面,计数将显示为“3”。 - 有趣的是,每次我尝试上述操作并在点击后退按钮后返回应用程序时,计数都会增加 1 次。

第二个问题是,当我返回应用程序并且计数显示为“5”时,“重置”按钮不再起作用 - 它什么也不做。但是当我继续计数然后点击重置按钮时,它会再次将计数更改为零。

请问有人可以协助解决上述两个问题吗?

建议后的新代码:

public class wazeefa extends Activity  {

//Count Button
TextView txtCount;
Button btnCount;
Button wmute;
Button wreset;
public static int count=0;
SharedPreferences app_preferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wazeefa);

//SAVE COUNT
SharedPreferences app_preferences =
PreferenceManager.getDefaultSharedPreferences(this);

count = app_preferences.getInt("count", 0);

txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText("This app has been started " + count + " times.");}

protected void onPause() {
super.onPause();

// save count value here
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();

//SOUND and COUNT
final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.bubble);

txtCount = (TextView)findViewById(R.id.wcount);
txtCount.setText(String.valueOf(count));
btnCount = (Button)findViewById(R.id.wclick);

btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
final ImageView image = (ImageView) findViewById(R.id.imageview);
count++;
if (count > 50) count = 0; image.setImageResource(R.drawable.duroodimage);
if (count > 0) image.setImageResource(R.drawable.duroodimage);
if (count > 9) image.setImageResource(R.drawable.zikrimage);
if (count > 39) image.setImageResource(R.drawable.duroodimage);
txtCount.setText(String.valueOf(count));
mpButtonClick.start();

//RESET Button
wreset = (Button)findViewById(R.id.wreset);
wreset.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
count = 0;
image.setImageResource(R.drawable.duroodimage);;
txtCount.setText("0");


}

我的代码的最后部分:

 protected void onPause() {
super.onPause();

// save count value here

SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();


};
});
}});
};}

最佳答案

在类级别将计数声明为静态:

public static int count=0;
SharedPreferences app_preferences ;

并使用onPause将计数值保存在SharedPreferences中:

 protected void onPause() {
super.onPause();

// save count value here
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
}

关于java - 共享首选项不保存 Android 应用程序的确切实例状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14021477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com