onSavedInstanceState and onRestoreInstanceState in activity Android

The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams. So we solve previous year question papers for you.
In eduladder you can Ask,Answer,Listen,Earn and Download Questions and Question papers.
Watch related videos of your favorite subject.
Connect with students from different parts of the world.
Apply or Post Jobs, Courses ,Internships and Volunteering opportunity. For FREE
See Our team
Wondering how we keep quality?
Got unsolved questions? Ask Questions

Android-->View question

onSavedInstanceState() and onRestoreInstanceState() in activity (Android)?

onSavedInstanceState() and onRestoreInstanceState() in activity?



Asked On2019-09-10 16:22:28 by:Tejasurve

Taged users:
Annaliyammodi1995harshshah822RSHAH12iamprincerajTejasurveShwetaMerchantOshal-BorkarAkhil-Rajprajwalamv

Likes:
Be first to like this question

Dislikes:
Be first to dislike this question
Talk about this  Like  Dislike
View all qusetions
Answers

They are used to save and retrieve values. The values are stored in the form of a key-value pair.
Let’s look at each of them separately.

onSaveInstanceState

onSaveInstanceState method gets called typically before/after onStop() is called. This varies from Android version to version. In the older versions it used to get before onStop().

Inside this method, we save the important values in the Bundle in the form of key value pairs.

The onSaveInstanceState method looks like :


@Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

On the outState bundle instance we add the key value pairs. Following are the methods applicable:

android onsaveinstancestate methods

We can pass custom class instances by setting the class as Parcelable or Serializable.

Let’s look at an example using Parcelable since it is faster than Serializable.

Following is a custom class :


public class Model implements Parcelable {
        public long id;
        public String name;

        public Model(long id, String name) {
            this.id = id;
            this.name = name;
        }

        protected Model(Parcel in) {
            id = in.readLong();
            name = in.readString();
        }

        public final Creator<Model> CREATOR = new Creator() {
            @Override
            public Model createFromParcel(Parcel in) {
                return new Model(in);
            }

            @Override
            public Model[] newArray(int size) {
                return new Model[size];
            }
        };

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeLong(id);
            parcel.writeString(name);
        }
    }

writeToParcel method is where we set the class properties on the Parcelable instance.

Now create an instance of the Model and save it in onSaveInstanceState.



Model model = new Model(10, "Hello");

@Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable("parcelable", model);
    }

Now we can retrieve these saved values in the onRestoreInstanceState method.

Note: onSaveInstanceState gets called whenever you press the home button in your application as well.

Another note: Things like EditText can save and restore their content implicitly provided you’ve set an id on the View. Activity will automatically collect View’s State from every single View in the View hierarchy.


onRestoreInstanceState

This method gets triggered only when something was saved in onSaveInstanceState method.
It gets called after onStart().


@Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Model model = savedInstanceState.getParcelable("parcelable");
    }

Following are the other method available:

android onrestoreinstancestate methods

The super method implementation restores the view hierarchy.

Generally, onRestoreInstanceState isn’t used to restore values often now. The same can be done from the Bundle in the onCreate method. Also since the onCreate method is always called before, it is a good practice to restore the saved instances there only.

In the following section, we’ll be creating a Counter Application in which we’ll save the state of the counter using the above methods.

Project Structure


Answerd on:2019-09-11 Answerd By:Tanisha-Garg

Likes:
Be first to like this answer

Dislikes:
Be first to dislike this answer
Talk about this  Like  Dislike

You might like this video:Data Flow on the Internet
Watch more here

Watch more videos from this user Here

Learn how to upload a video and start earning here



Lets together make the web is a better place

We made eduladder by keeping the ideology of building a supermarket of all the educational material available under one roof. We are doing it with the help of individual contributors like you, interns and employees. So the resources you are looking for can be easily available and accessible also with the freedom of remix reuse and reshare our content under the terms of creative commons license with attribution required close.

You can also contribute to our vision of "Helping student to pass any exams" with these.
Answer a question: You can answer the questions not yet answered in eduladder.How to answer a question
Career: Work or do your internship with us.Work with us
Create a video: You can teach anything and everything each video should be less than five minutes should cover the idea less than five min.How to upload a video on eduladder