开发者

Save object while orientation change

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

developer site say

Use the new Fragment API setRetainInstance(boolean) instead;

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

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

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?

EDIT: 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 ; 
    ...开发者_如何学Go.............

I'm looking, is there any alternatives for onRetainNonConfigurationInstance method, which save an object completely while orientation change and later can be retrieve using getLastNonConfigurationInstance


You can use onRetainCustomNonConfigurationInstance.

Use this instead of onRetainNonConfigurationInstance(). Retrieve later with getLastCustomNonConfigurationInstance().


There are two alternatives:

  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.


When your Fragment is paused it will call this method:

@Override
public void onSaveInstanceState(Bundle outState) {
    // Add variable to outState here
    super.onSaveInstanceState(outState);
}

The variable outState will then be fed into the onCreate() method when the Fragment restarts.

You can save any data that is a basic type or implements Parcelable interface


Well, quote from Android Developers' References:

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).

Regarding the use of onSaveInstanceState(), it's better to revert your objects/things by using onRestoreInstanceState() instead.

Refer to Activity#onRestoreInstanceState()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜