Restoring State after Orientation Change in Loklak Wok Android
During orientation change i.e. from portrait to landscape mode in Android, the current activity restarts again. As the activity restarts again, all the defined variables loose their previous value, for example the scroll position of a RecyclerView, or the data in the rows of RecyclerView etc. Just imagine a user searched some tweets in Loklak Wok Android, and as the user’s phone is in “Auto rotation” mode, the orientation changes from portrait to landscape. As a result of this, the user loses the search result and has to do the search again. This leads to a bad UX.
Saving state in onSavedInstanceState
The state of the app can be saved by inserting values in a Bundle object in onSavedInstanceState callback. Inserting values is same as adding elements to a Map in Java. Methods like putDouble, putFloat, putChar etc. are used where the first parameter is a key and the second parameter is the value we want to insert.
@Override public void onSaveInstanceState(Bundle outState) { if (mLatitude != null && mLongitude != null) { outState.putDouble(PARCELABLE_LATITUDE, mLatitude); outState.putDouble(PARCELABLE_LONGITUDE, mLongitude); } ... }
The values can be retrieved back when onCreate or onCreateView of the Activity or Fragment is called. Bundle object in the callback parameter is checked, whether it is null or not, if not the values are retrieved back using the keys provided at the time of inserting. The latitude and longitude of a location in TweetPostingFragment are retrieved in the same fashion
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { ... if (savedInstanceState != null) { // checking if bundle is null // extracting from bundle mLatitude = savedInstanceState.getDouble(PARCELABLE_LATITUDE); mLongitude = savedInstanceState.getDouble(PARCELABLE_LONGITUDE); // use extracted value } }
Restoring Custom Objects, using Parcelable
But what if we want to restore custom object(s). A simple option can be serializing the objects using the native Java Serialization or libraries like Gson. The problem in these cases is performance, they are quite slow. Parcelable can be used, which leads the pack in performance and moreover it is provided by Android SDK, on top of that, it is simple to use.
The objects of class which needs to be restored implements Parcelable interface and the class must provide a static final object called CREATOR which implements Parcelable.Creator interface.
writeToParcel and describeContents method need to be override to implement Parcelable interface. In writeToParcel method the member variables are put inside the parcel, in our case describeContents method is not used, so, simply 0 is returned. Status class which stores the data of a searched tweet implements parcelable.
@Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(mText); dest.writeInt(mRetweetCount); dest.writeInt(mFavouritesCount); dest.writeStringList(mImages); dest.writeParcelable(mUser, flags); }
NOTE: The order in which variables are pushed into Parcel needs to be maintained while variables are extracted from the parcel to recreate the object. This is the reason why no “key” is required to push data into a parcel as we do in bundle.
The CREATOR object implements the creation of object from a Parcel. The CREATOR object overrides two methods createFromParcel and newArray. createFromParcel is the method in which we implement the way an object is created from a parcel.
public static final Parcelable.Creator<Status> CREATOR = new Creator<Status>() { @Override public Status createFromParcel(Parcel source) { return new Status(source); // a private constructor to create object from parcel } @Override public Status[] newArray(int size) { return new Status[size]; } };
The private constructor, note that the order in which variables were pushed is maintained while retrieving the values.
private Status(Parcel source) { mText = source.readString(); mRetweetCount = source.readInt(); mFavouritesCount = source.readInt(); mImages = source.createStringArrayList(); mUser = source.readParcelable(User.class.getClassLoader()); }
The status objects are restored the same way, latitude and longitude were restored. putParcelableArrayList in onSaveInstaceState and getParcelableArrayList in onCreateView methods are used to push into Bundle object and retrieve from Bundle object respectively.
@Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); ArrayList<Status> searchedTweets = mSearchCategoryAdapter.getStatuses(); outState.putParcelableArrayList(PARCELABLE_SEARCHED_TWEETS, searchedTweets); ... } // retrieval of the pushed values in bundle @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ... if (savedInstanceState != null) { ... List<Status> searchedTweets = savedInstanceState.getParcelableArrayList(PARCELABLE_SEARCHED_TWEETS); mSearchCategoryAdapter.setStatuses(searchedTweets); } ... return view; }
Resources:
- Parcelable v/s Serializable: https://www.3pillarglobal.com/insights/parcelable-vs-java-serialization-in-android-app-development
- Parcelable tutorial: http://www.vogella.com/tutorials/AndroidParcelable/article.html
You must be logged in to post a comment.