我正在编写一个 Android 测验应用程序, 现在每次用户回答错误时,都会出现卡住计时器,用户必须等到他能够再次回答时,我的问题如下:
每次用户回答错误并且计时器出现时,他可以通过按返回按钮返回主菜单然后按问题按钮重新启动整个过程。
我需要有关 onSaveInstanceState
和 onRestoreInstanceState
函数的帮助。
代码是:
public class Questionsctivity extends Activity {
List<Question> quesList;
int score = 0;
int qid = 0;
Question currentQ;
TextView txtQuestion,clockView;
RadioButton rda, rdb, rdc;
Button butNext;
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questionsctivity);
DbHelper db = new DbHelper(this);
quesList = db.getAllQuestions();
currentQ = quesList.get(qid);
clockView = (TextView) findViewById(R.id.textView222);
txtQuestion = (TextView) findViewById(R.id.textView1);
rda = (RadioButton) findViewById(R.id.radio0);
rdb = (RadioButton) findViewById(R.id.radio1);
rdc = (RadioButton) findViewById(R.id.radio2);
butNext = (Button) findViewById(R.id.next);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
if (currentQ.getANSWER().equals(answer.getText())) {//bad equal to
score++;
Log.d("Good Answer", "Your Score" + score);
} else {
Log.d("bad Answer", "Your Score" + score);
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
clockView.setText("Wrong Answer +" +
"seconds remaining: " + millisUntilFinished / 1000);
butNext.setEnabled(false);
txtQuestion.setEnabled(false);
rda.setEnabled(false);
rdb.setEnabled(false);
rdc.setEnabled(false);
}
public void onFinish() {
clockView.setText("Continue Answering!");
butNext.setEnabled(true);
txtQuestion.setEnabled(true);
rda.setEnabled(true);
rdb.setEnabled(true);
rdc.setEnabled(true);
}
}.start();
}
if (qid < 7) {
currentQ = quesList.get(qid);
setQuestionView();
} else {
Intent intent = new Intent(Questionsctivity.this, ResultActivity.class);
Bundle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
private void setQuestionView() {
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
qid++;
}
@Override
public void onStart() {
super.onStart();
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Questionsctivity Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.ourapp.myapplication/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Questionsctivity Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.ourapp.myapplication/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
最佳答案
您必须使用 onPause()
方法来保存 Activity 的状态,因为只有在系统即将销毁时才会调用 onSaveInstanceState()
Activity(如果需要释放内存)。如果用户按下后退(或主页)按钮关闭当前 Activity,则不会调用 onSaveInstanceState()
。
例如,您可以将当前状态保留在共享首选项或本地数据库中。
然后您可以在onResume()
方法中恢复保存的状态。
关于java - 按下后退按钮时如何保存状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34993677/