Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6554141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:39:54+00:00 2026-05-25T12:39:54+00:00

This is follow up to: Android local variable get's lost when using camera intent

  • 0

This is follow up to: Android local variable get's lost when using camera intent

Proper way to do it is to handle onSaveInstanceState and onRestoreInstanceState like shown here:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/CompoundButton.java

Here is my code:

static class SavedState extends BaseSavedState
    {
        private String requestedFileName;
        private UUID[] mImages = new UUID[4];

        SavedState(Parcelable superState)
        {
          super(superState);
        }

        private SavedState(Parcel in)
        {
          super(in);
        }

        @Override
        public void writeToParcel(Parcel out, int flags)
        {
          super.writeToParcel(out, flags);
        }

        //required field that makes Parcelables from a Parcel
        public static final Parcelable.Creator<SavedState> CREATOR =
            new Parcelable.Creator<SavedState>()
            {
              public SavedState createFromParcel(Parcel in)
              {
                return new SavedState(in);
              }
              public SavedState[] newArray(int size)
              {
                return new SavedState[size];
              }
        };

    }

    @Override
    public Parcelable onSaveInstanceState()
    {
        //begin boilerplate code that allows parent classes to save state
        Parcelable superState = super.onSaveInstanceState();

        SavedState ss = new SavedState(superState);
        //end

        ss.requestedFileName = this.requestedFileName;
        ss.mImages = this.mImages;

        return ss;
    }

    @Override
    public void onRestoreInstanceState(Parcelable state)
    {
        //begin boilerplate code so parent classes can restore state
        if(!(state instanceof SavedState))
        {
            super.onRestoreInstanceState(state);
            return;
        }

        SavedState ss = (SavedState)state;
        super.onRestoreInstanceState(ss.getSuperState());
        //end

        this.requestedFileName = ss.requestedFileName;
        this.mImages = ss.mImages;

        RestoreImageState();
    }

Now to my question. This code seems to work properly and it handles all state changes without a problem. HOWEVER, if you look at SavedState.writeToParcel and SavedState.SavedState you will notice that I do not store my variables there. Do I have to? Why? Problem is that I understand how to wrteToParcel and my data types match. But reading out of parcel not so clear with complex types. And in my tests it wasn’t called.

EDIT:

Does this look correct for save/retreive order?

private SavedState(Parcel in)
        {
          super(in);
          this.mName = in.readString();
          this.mIndex = in.readInt();
          this.mApplicableDamages = (String[])in.readValue(null);
          this.mSelectedDamages = (boolean[])in.readValue(null);                            
        }

        @Override
        public void writeToParcel(Parcel out, int flags)
        {
          super.writeToParcel(out, flags);
          out.writeString(this.mName);
          out.writeInt(this.mIndex);
          out.writeArray(this.mApplicableDamages);
          out.writeBooleanArray(this.mSelectedDamages);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:39:55+00:00Added an answer on May 25, 2026 at 12:39 pm

    You have to save these variables in onSaveInstanceState() and restore them in onRestoreInstanceState() later if they’re part of your view’s state and you don’t want to lose them when a parent activity is recreated.

    The variables may be lost because when the configuration change happens the parent activity is destroyed and a new activity object is recreated. This new object is receive the previously saved state. And if you don’t add some variables to the state the won’t be restored.

    As to the writing complex types to and reading the from Parcel you can implement Parcelable for some parts of the complex type. Or you can just decompose the complex type to the primitive (parcelable) fields and store this fields one by one. In your case UUID[] can be stored in Parcel as Serializable or you can convert UUID[] to ParcelUuid[] and store this array as parcelable array.

    And a few words about SavedState implementation. The fields in SavedState will not be saved if you don’t add them to Parcel. So you have to write the to Parcel in SavedState.writeToParcel() method. And also you need to read them back in SavedState(Parcel) constructor.

    static class SavedState extends BaseSavedState
    {
        private String requestedFileName;
        private UUID[] mImages = new UUID[4];
    
        SavedState(Parcelable superState)
        {
            super(superState);
        }
    
        private SavedState(Parcel in)
        {
            super(in);
            requestedFileName = in.readString();
            mImages = (UUID[])in.readSerializable();
        }
    
        @Override
        public void writeToParcel(Parcel out, int flags)
        {
            super.writeToParcel(out, flags);
            out.writeString(requestedFileName);
            out.writeSerializable(mImages);
        }
    
        //required field that makes Parcelables from a Parcel
        public static final Parcelable.Creator<SavedState> CREATOR =
            new Parcelable.Creator<SavedState>()
            {
                public SavedState createFromParcel(Parcel in)
                {
                    return new SavedState(in);
                }
    
                public SavedState[] newArray(int size)
                {
                    return new SavedState[size];
                }
            };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to follow the Android mapping tutorial and got to this part
This is the design decision I don't understand. Both Android and JME follow the
So I'm trying to follow this tutorial... to learn android, and I'm having a
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
This is a follow on question to How do I delete 1 file from
This is a follow-on question to the How do you use ssh in a
This is a follow up question to This Question . I like (and understand)
This is a follow-up to the question: Should the folders in a solution match
This is a follow on to this question . I am trying to avoid
This is a follow-up to a previous question I had about interfaces. I received

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.