简体   繁体   English

Android:“返回”按钮中的onSaveInstanceState

[英]Android: onSaveInstanceState in Back button

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. 请给我建议。

Now but from second activity if i click on device back button the state is not maintained. 现在但是从第二次活动开始,如果我单击设备后退按钮,则状态不会保持。

onSaveInstanceState() is mostly used for configuration changes (eg, rotating the screen). onSaveInstanceState()通常用于配置更改(例如,旋转屏幕)。

So what should i do to maintain the state on back button 所以我该怎么做才能保持后退按钮上的状态

Most likely, there is no "state" that needs to be "maintained", outside of your data model. 最有可能的是,在数据模型之外,没有“状态”需要“维护”。 Your activity needs to update its data model: files, database, preferences, ContentProvider , some singleton that is your in-memory data model manager, whatever. 您的活动需要更新其数据模型:文件,数据库,首选项, ContentProvider ,一些作为内存数据模型管理器的单例(无论如何)。

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 ie if the check box was checked then it should remain checked. 现在,如果您按新活动上的“后退”按钮,则第一个活动应以其当前状态出现(如果尚未销毁,请调用finish()),即如果该复选框已选中,则应保留该活动检查。 You don't need to save the activity state unless the orientation is changed or the activity is destroyed. 除非更改方向或破坏活动,否则无需保存活动状态。

Are you sure you are not doing any thing in the onActivityResult or onResume() method which effects the state of the check box? 您确定在onActivityResult或onResume()方法中没有做任何事情会影响复选框的状态吗? 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? 您还可以确保在启动新活动之前,代码本身不会取消选中该复选框吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM