Android的:如何以节省屏幕方向变化的活动的实例?我使用的对象,不只是简单的变量 [英] Android: How to save instance of the activity on screen orientation change? I use objects, not just simple variables

查看:143
本文介绍了Android的:如何以节省屏幕方向变化的活动的实例?我使用的对象,不只是简单的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在屏幕上的方向变化保存实例的问题。我有具有特定结构的阵列:

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:

  • 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() in onCreate() after rotation;

这篇关于Android的:如何以节省屏幕方向变化的活动的实例?我使用的对象,不只是简单的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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