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
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.
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.
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.
You can use onRetainCustomNonConfigurationInstance.
Use this instead of onRetainNonConfigurationInstance(). Retrieve later with getLastCustomNonConfigurationInstance().
There are two alternatives:
Loader
. The FragmentActivity
will take care of saving/restoring its state when re-creating.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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With