Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ; 
    ................

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

like image 509
Labeeb Panampullan Avatar asked Oct 06 '11 12:10

Labeeb Panampullan


People also ask

How do you change orientation when retaining data?

Another most common solution to dealing with orientation changes by setting the android:configChanges flag on your Activity in AndroidManifest. xml. Using this attribute your Activities won't be recreated and all your views and data will still be there after orientation change.

What happens on orientation change?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

What happens when screen rotates Android?

When screen rotates, Android destroys the current activity and recreates it. Which means the Activity methods will be called in following order. When onCreate method is called we call setContentView(R. layout.


3 Answers

You can use onRetainCustomNonConfigurationInstance.

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

like image 180
Giorgi Avatar answered Oct 02 '22 03:10

Giorgi


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.
like image 35
Nikolay Elenkov Avatar answered Oct 02 '22 03:10

Nikolay Elenkov


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

like image 21
Graeme Avatar answered Oct 02 '22 03:10

Graeme