嘿,我正在尝试制作自己的应用程序,我已经在网上搜索了信息,但我不明白其他人认为有帮助的解决方案。但我有一个小问题。我希望应用程序在关闭应用程序时保存布尔值 stopValue,但是当我尝试此操作时,它不会在我关闭应用程序时保存该值,而是将值设置为 true。
我该如何解决?
谢谢你的帮助。
public class Main extends Activity{
Button bStart, bStop;
TextView tvDate, tvKm;
Spinner spinner1;
boolean stopValue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bStart = (Button) findViewById(R.id.bStart);
tvDate = (TextView) findViewById(R.id.tvDate);
tvKm = (TextView) findViewById(R.id.tvKm);
spinner1 = (Spinner) findViewById(R.id.spinner1);
boolean stopValue = false;
SharedPreferences sp = getApplicationContext().getSharedPreferences("stopValue", 1);
stopValue = sp.getBoolean("stop", false);
stopValue = getIntent().getBooleanExtra("stop", stopValue);
if(stopValue){
bStart.setText("Start");
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent start = new Intent("com.uniqueapps.runner.START");
startActivity(start);
}
});
}
if(stopValue == false){
bStart.setText("Stop");
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent stop = new Intent("com.uniqueapps.runner.STOP");
startActivity(stop);
}
});
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
SharedPreferences sp = getApplicationContext().getSharedPreferences("stopValue", 1);
Editor edit = sp.edit();
edit.putBoolean("stop", true);
edit.commit();
}