The Eduladder is a community of students, teachers, and programmers just interested to make you pass any exams. So we help you to solve your academic and programming questions fast.
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:

Be first to like this question

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

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 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 :

    public void onSaveInstanceState(Bundle 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) {
   = id;
   = name;

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

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

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

        public int describeContents() {
            return 0;

        public void writeToParcel(Parcel parcel, int i) {

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");

    public void onSaveInstanceState(Bundle 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.


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

    protected void onRestoreInstanceState(Bundle 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

Be first to like this answer

Be first to dislike this answer
Talk about this  Like  Dislike

You might like this video: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