Java 按下后退按钮时如何保存状态
我正在写一个android问答应用程序, 现在,每当用户回答错误时,就会出现一个冻结计时器,用户必须等待,直到他能够再次回答,我的问题如下: 每次用户回答错误并且计时器出现时,他都可以通过按主菜单上的“后退”按钮,然后按“问题”按钮来重新启动整个程序 我需要有关SaveInstanceState和RestoreInstanceState函数的帮助 代码是:Java 按下后退按钮时如何保存状态,java,android,Java,Android,我正在写一个android问答应用程序, 现在,每当用户回答错误时,就会出现一个冻结计时器,用户必须等待,直到他能够再次回答,我的问题如下: 每次用户回答错误并且计时器出现时,他都可以通过按主菜单上的“后退”按钮,然后按“问题”按钮来重新启动整个程序 我需要有关SaveInstanceState和RestoreInstanceState函数的帮助 代码是: public class Questionsctivity extends Activity { List<Question
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;
int-qid=0;
问题Q;
TextView-txtQuestion,clockView;
单选按钮rda、rdb、rdc;
按钮butNext;
私人谷歌客户;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u questions活动);
DbHelper db=新的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=(按钮)findViewById(R.id.next);
setQuestionView();
但是next.setOnClickListener(newview.OnClickListener(){
@凌驾
公共void onClick(视图v){
放射组grp=(放射组)findViewById(R.id.RadioGroup 1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d(“yourans”,currentQ.getANSWER()+“”+answer.getText());
如果(currentQ.getANSWER().equals(answer.getText()){//bad等于
分数++;
Log.d(“回答正确”,“你的分数”+分数);
}否则{
Log.d(“回答不好”,“你的分数”+分数);
新的倒计时(300001000){
公共void onTick(长毫秒未完成){
clockView.setText(“错误答案+”+
“剩余秒数:”+millisnsuntillfinished/1000);
butNext.setEnabled(false);
txtQuestion.setEnabled(false);
rda.setEnabled(false);
rdb.setEnabled(false);
rdc.setEnabled(false);
}
公共无效onFinish(){
clockView.setText(“继续应答!”);
butNext.setEnabled(true);
txtQuestion.setEnabled(true);
rda.setEnabled(真);
rdb.setEnabled(true);
rdc.setEnabled(true);
}
}.start();
}
如果(qid<7){
currentQ=quesList.get(qid);
setQuestionView();
}否则{
意向意向=新意向(QuestionsActivity.this、ResultActivity.class);
Bundle b=新Bundle();
b、 putInt(“分数”,分数);//你的分数
意图。附加动作(b);//把你的分数放在下一个意图上
星触觉(意向);
完成();
}
}
});
client=new GoogleApiClient.Builder(this.addApi(AppIndex.API).build();
}
私有void setQuestionView(){
setText(currentQ.getQUESTION());
setText(currentQ.getOPTA());
setText(currentQ.getOPTB());
setText(currentQ.getOPTC());
qid++;
}
@凌驾
public void onStart(){
super.onStart();
client.connect();
Action viewAction=Action.newAction(
Action.TYPE\u视图,//TODO:选择一种操作类型。
“QuestionsActivity页面”//TODO:为显示的内容定义标题。
//TODO:如果您的网页内容与此应用程序活动的内容匹配,
//确保此自动生成的网页URL正确无误。
//否则,请将URL设置为null。
解析http://host/path"),
//TODO:确保此自动生成的应用程序深度链接URI正确。
解析-app://com.example.ourapp.myapplication/http/host/path")
);
AppIndex.AppIndexApi.start(客户端,viewAction);
}
@凌驾
公共void onStop(){
super.onStop();
Action viewAction=Action.newAction(
Action.TYPE\u视图,//TODO:选择一种操作类型。
“QuestionsActivity页面”//TODO:为显示的内容定义标题。
//TODO:如果您的网页内容与此应用程序活动的内容匹配,
//确保此自动生成的网页URL正确无误。
//否则,请将URL设置为null。
解析http://host/path"),
//TODO:确保此自动生成的应用程序深度链接URI正确。
解析-app://com.example.ourapp.myapplication/http/host/path")
);
AppIndex.AppIndexApi.end(客户端,viewAction);
client.disconnect();
}
}
使用以下选项禁用后退按钮或根据您的要求使用
@Override
public void onBackPressed() {
super.onBackPressed();
// use your own custom method or you can remove super.onBackPressed(); to disable back button.
}
您必须使用
onPause()
方法来保存活动的状态,因为只有在系统要销毁活动的情况下才会调用onSaveInstanceState()
(在需要释放内存的情况下)。onSaveInstanceState()
@Override
public void onBackPressed() {
Bundle bundle = new Bundle();
bundle.putString(key, value);
onSaveInstanceState(bundle);
super.onBackPressed(); //Check if you still want to go back
}