保存而方向改变的对象 [英] Save object while orientation change

查看:93
本文介绍了保存而方向改变的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何保存对象,而方向的变化,因为<一href="http://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance%28%29">onRetainNonConfigurationInstance和<一href="http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance%28%29">getLastNonConfigurationInstance是德precated。而且不能给我兼容包使用 Android的支持 - v4.jar FragmentActivity ,它显示了无法覆盖从FragmentActivity的最终方法​​

How to save Object while orientation change, since onRetainNonConfigurationInstance and getLastNonConfigurationInstance are deprecated. And which cannot me used with compatibility package android-support-v4.jar FragmentActivity, where it shows Cannot override the final method from FragmentActivity

开发者网站说

使用新的片段API setRetainInstance(布尔)代替;

Use the new Fragment API setRetainInstance(boolean) instead;

但我不知道如何保存使用自定义对象<一href="http://developer.android.com/reference/android/app/Fragment.html#setRetainInstance%28boolean%29">setRetainInstance

But I don't know how to save a custom object using setRetainInstance

我的情况:
在我的活动我有一个进度对话框一个AsyncTask的。在这里,我需要处理的方向改变。
对于我从马克·墨菲一个很好的答案,CommonsWare
<一href="http://stackoverflow.com/questions/3821423/background-task-progress-dialog-orientation-change-is-there-any-100-working/3821998#3821998">background-task-progress-dialog-orientation-change-is-there-any-100-working,
示例项目

My scenario :
In my activity I have a AsyncTask with progress dialog. Here I need to handle orientation change.
For that I got a very good answer from Mark Murphy, CommonsWare
background-task-progress-dialog-orientation-change-is-there-any-100-working,
with sample project

由于我使用的是兼容包Android的支持 - v4.jar, FragmentActivity ,我不能重写 onRetainNonConfigurationInstance
无法从FragmentActivity覆盖的最终方法​​

Since I'm using compatibility package android-support-v4.jar, FragmentActivity, I can't override onRetainNonConfigurationInstance
Cannot override the final method from FragmentActivity

有救了我的自定义对象的其他方法?

Is there any alternative method for saving my custom object?

编辑: 我不能让我的AsyncTask的任务Parcelable(如果我没看错),因为它使用的界面,背景等。 我的的AsyncTask

I cannot make my AsyncTask task Parcelable (If I'm not wrong) since it use interface, context etc. My AsyncTask

 public class CommonAsyncTask extends AsyncTask<Object, Object, Object>  {
        Context context;
        AsyncTaskServices callerObject;
        ProgressDialog progressDialog;
        String dialogMessag ; 
    ................

我在寻找,是否有onRetainNonConfigurationInstance 方式的任何的替代品,它完全保存对象,而方向变化以后可以检索使用 getLastNonConfigurationInstance

推荐答案

有两种选择:

  1. 使用一个装载机。该 FragmentActivity 会照顾节省/恢复其状态时,重新创建。
  2. 使用的片段,不设窗户,并调用 setRetainInstance(真)就可以了。还有就是这个在兼容性库的源代码,FragmentRetainInstanceSupport或一些这样的。
  3. 为例
  1. Use a Loader. The FragmentActivity will take care of saving/restoring its state when re-creating.
  2. Use a fragment without a view and call setRetainInstance(true) on it. There is an example of this in the compatibility library's source, FragmentRetainInstanceSupport or some such.

这篇关于保存而方向改变的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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