我正在编写一个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();
}
}
答案 0 :(得分:1)
您只需使用手动创建的onSaveInstanceState
手动调用Bundle
。
@Override
public void onBackPressed() {
Bundle bundle = new Bundle();
bundle.putString(key, value);
onSaveInstanceState(bundle);
super.onBackPressed(); //Check if you still want to go back
}
答案 1 :(得分:0)
使用以下内容禁用后退按钮或根据您的要求使用。
@Override
public void onBackPressed() {
super.onBackPressed();
// use your own custom method or you can remove super.onBackPressed(); to disable back button.
}
答案 2 :(得分:0)
您必须使用onPause()
方法来保存Activity的状态,因为只有在系统要销毁Activity时才会调用onSaveInstanceState()
(如果内存需要是释放)。如果用户按下后退(或主页)按钮以关闭当前活动,则不会调用onSaveInstanceState()
。
例如,您可以将当前状态保留在共享首选项或本地数据库中。
然后,您可以使用onResume()
方法恢复已保存的状态。