安卓:在的onSaveInstanceState后退按钮 [英] Android: onSaveInstanceState in Back button

查看:186
本文介绍了安卓:在的onSaveInstanceState后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发中,我正在重写后退按钮的应用程序。我创建了一个复选框。在点击其中我呼吁意图:

I am developing an application in which i am overriding the back button. I created a check box. On click of which i am calling intent for:

startActivityforResult();

和还保持活动的状态为:

And also maintaining the state of activity as :

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putBoolean("checkbox", checkbox.isChecked());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  checkbox_state = savedInstanceState.getBoolean("checkbox");
}

其运行正常,并保持该状态。
意味着我在编辑文本输入值。和复选框,点击呼吁的结果,而在第一项活动回报状态是维持新的活动。

which run fine and the state is maintained. Means i am entering value in edit text. and on check box click calling new activity for result and while return on first activity the state is maintain.

现在不过从第二个活动,如果我点击设备后退按钮状态不能保持。

Now but from second activity if i click on device back button the state is not maintained.

所以,我应该怎么做才能维持后退按钮的状态。我搜查,但没有找到满意的解决方案。请给我建议。

So what should i do to maintain the state on back button. I searched but not found satisfied solution. Please suggest me.

推荐答案

当你开始新的活动,那么当前的活动被隐藏起来,新的活动被放置在堆栈的顶部。现在,如果你preSS对新的活动后退按钮,然后第一个活动应该拿出其当前状态(如果它不被破坏,呼吁结束()),它被留下,即如果被选中的复选框,然后应选中。除非方位被改变或活性被破坏不需要保存的活动状态。

When You start the new Activity then the current Activity gets hidden and new Activity is placed on top of the stack. Now if you press the back button on the new Activity then the first activity should come up in its current state (If it wasn't destroyed, calling finish()) where it was left i.e. if the check box was checked then it should remain checked. You don't need to save the activity state unless the orientation is changed or the activity is destroyed.

你确定你没有做任何事情,其中​​效果复选框的状态的onActivityResult或onResume()方法?我会建议先注释掉所有code在这两种方法,看看你的复选框保留状态。也可以还确保了code本身不会开始新的活动之前取消选中该复选框?

Are you sure you are not doing any thing in the onActivityResult or onResume() method which effects the state of the check box? I would recommend to first comment out all the code in both the methods and see if your check box retains the state. Also can you also make sure that the code itself doesn't uncheck the checkbox before starting the new Activity?

这篇关于安卓:在的onSaveInstanceState后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆