안녕하세요. 저는 이미 내 앱을 만들려고 노력하고 있습니다. 이미 웹에서 정보를 검색했으며 다른 사람들이 유용하다고 생각한 솔루션을 이해하지 못했습니다. 하지만 약간 문제가있어. 내가 응용 프로그램을 닫을 때 부울 값인 stopValue를 저장하는 응용 프로그램을 원하지만이 작업을 시도 할 때 응용 프로그램을 닫을 때 값을 저장하지 않고 값을 true로 설정합니다.불리언 및 sharedpreference
어떻게 수정합니까?
도움 주셔서 감사합니다.
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();
}
그것은 작동합니다 .. 고맙습니다. 일시 중지 메서드가 값을 저장하기 전에 호출 되었기 때문입니까? – Niller
예, 프레임 워크는 super.onPause()를 호출 할 때 액티비티를 일시 중지합니다. – Rasmus