Android的:如何以节省屏幕方向变化的活动的实例?我使用的对象,不只是简单的变量 [英] Android: How to save instance of the activity on screen orientation change? I use objects, not just simple variables
问题描述
我有一个在屏幕上的方向变化保存实例的问题。我有具有特定结构的阵列:
I have a problem with saving an instance on screen orientation change. I have an array with a specific structure:
private ArrayList <MyObject> myArr;
这sctructure是:
And this sctructure is:
public MyObject{
public variable1;
//..... many variables here and one array :)
}
我需要保存myArr,该阵列。我该如何拯救这个变量?
I need to save "myArr" array. How can I save this "variable"?
推荐答案
基本上,比如应保存,使用方式,在的 Android的活动API参考:你需要使用的onSaveInstanceState(捆绑)
。另外,你可以只设置安卓configChanges =定向
在清单的活动,以prevent它是娱乐(所以在这里将不会有任何需要国家储蓄presented)。
Basically, instance should be saved, using the way, described in Android Activity API Ref: you need to use onSaveInstanceState(Bundle)
. Also, You can just set android:configChanges="orientation"
in your manifest for an activity to prevent it's recreation (so where won't be any need for state saving presented).
要保存的问题对象的onSaveInstanceState(捆绑)
的软件包:
To save objects from the question in onSaveInstanceState(Bundle)
to the Bundle:
- 实施 Parcelable 通过
MyObject来
(如例子可以找到这里等。) - 在束将通过的ArrayList <一href="http://developer.android.com/reference/android/os/Bundle.html#putParcelableArrayList%28java.lang.String,%20java.util.ArrayList%3C?%20extends%20android.os.Parcelable%3E%29"相对=nofollow> putParcelableArrayList()在
的onSaveInstanceState()
并取回由<一个href="http://developer.android.com/reference/android/os/Bundle.html#getParcelableArrayList%28java.lang.String%29v"相对=nofollow> getParcelableArrayList()在的onCreate()
旋转后;
- Implement Parcelable by
MyObject
(e.g. example can be found here etc.) - Put ArrayList in the bundle by putParcelableArrayList() in
onSaveInstanceState()
and get back by getParcelableArrayList() inonCreate()
after rotation;
这篇关于Android的:如何以节省屏幕方向变化的活动的实例?我使用的对象,不只是简单的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!