简体   繁体   English

Android在方向更改时保存状态

[英]Android save state on orientation change

I've got an Android application which maintains state regarding distance traveled, time elapsed, etc. This state I can conveniently store in an object and store a reference to that object in the Bundle when Android calls onDestroy() when the user changes the screen orientation, then restore the state in onCreate(Bundle savedBundle). 我有一个Android应用程序,该应用程序维护有关行进距离,经过时间等的状态。当用户更改屏幕时,当Android调用onDestroy()时,我可以方便地将该状态存储在对象中,并将对该对象的引用存储在Bundle中方向,然后在onCreate(Bundle savedBundle)中恢复状态。 However, I also have some state in the Buttons and EditText objects on the screen that I want to persist through screen orientations. 但是,我在屏幕上的Buttons和EditText对象中也有一些状态,我想通过屏幕方向保持该状态。 For example, in onStart(Bundle savedBundle) I call: 例如,在onStart(Bundle savedBundle)中,我调用:

_timerButton.setBackgroundColor(Color.GREEN);
_pauseButton.setBackgroundColor(Color.YELLOW);
_pauseButton.setEnabled(false);

Then throughout the operation of my app, the colors/enabled status of these buttons will be changed. 然后,在我的应用程序的整个操作过程中,这些按钮的颜色/启用状态将被更改。 Is there a more convenient way to persist the state of user interface items (EditText, Button objects, etc) without having to manually save/restore each attribute for each button? 是否有更方便的方法来持久保存用户界面项(EditText,Button对象等)的状态,而不必手动保存/恢复每个按钮的每个属性? It feels really clumsy to have to manually manage this type of state in between screen orientations. 必须在屏幕方向之间手动管理这种状态真的很笨拙。

Thanks for any help. 谢谢你的帮助。

Have you tried using: its work through 您是否尝试过使用:通过它的工作

<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/>

in Manifest file? 在清单文件中?

It does not work by default because , when you change the orientation onCreate will be called again and it redraws your view. 默认情况下它不起作用,因为,当您更改方向时,将再次调用onCreate并重绘视图。

If you write this parameter no need to handle in Activity , the framework will take care of rest of things. 如果您在Activity中无需处理此参数,则框架将处理其余工作。 It will retain the state of the screen or layout if orientation is changed. 如果更改方向,它将保留屏幕或布局的状态。

NOTE If you are using a different layout for landscape mode , by adding these parameters the layout for landscape mode will not be called. 注意如果横向模式使用其他布局,则通过添加这些参数,不会调用横向模式的布局。

Other way and Another way 另一种方法另一种方法

To save your variable or values you should use onSaveInstanceState(Bundle); 要保存变量或值,应使用onSaveInstanceState(Bundle);。 and when orientation changes then should recover values should use onRestoreInstanceState() as well, but not very common. 并且当方向更改时,应该恢复的值也应该使用onRestoreInstanceState(),但不是很常见。 ( onRestoreInstanceState() is called after onStart(), whereas onCreate() is called before onStart() . Use the put methods to store values in onSaveInstanceState() onRestoreInstanceState()在onStart()之后调用,而onCreate()在onStart()之前调用 。使用put方法将值存储在onSaveInstanceState()中。

protected void onSaveInstanceState(Bundle icicle) {
  super.onSaveInstanceState(icicle);
  icicle.putLong("param", value);
}

And restore the values in onCreate() : 并恢复onCreate()中的值:

public void onCreate(Bundle icicle) {
  if (icicle != null){
    value = icicle.getLong("param");
  }
}

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

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