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 8554557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:54:36+00:00 2026-06-11T14:54:36+00:00

I have an ArrayList with custom objects that I would like to be able

  • 0

I have an ArrayList with custom objects that I would like to be able to save and restore on a screen rotate.

I know that this can be done with onSaveInstanceState and onRestoreInstanceState if I were to make the ArrayList its own class, which implements either Parcelable or Serializable… But is there a way to do this without creating another class?

  • 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-06-11T14:54:37+00:00Added an answer on June 11, 2026 at 2:54 pm

    You do not need to create a new class to pass an ArrayList of your custom objects. You should simply implement the Parcelable class for your object and use Bundle#putParcelableArrayList() in onSaveInstanceState() and onRestoreInstanceState(). This method will store an ArrayList of Parcelables by itself.


    Because the subject of Parcelables (and Serializables and Bundles) sometimes makes my head hurt, here is a basic example of an ArrayList containing custom Parcelable objects stored in a Bundle. (This is cut & paste runnable, no layout necessary.)

    Implementing Parcelable

    public class MyObject implements Parcelable {
        String color;
        String number;
    
        public MyObject(String number, String color) {
            this.color = color;
            this.number = number;
        }
    
        private MyObject(Parcel in) {
            color = in.readString();
            number = in.readString();
        }
    
        public int describeContents() {
            return 0;
        }
    
        @Override
        public String toString() {
            return number + ": " + color;
        }
    
        public void writeToParcel(Parcel out, int flags) {
            out.writeString(color);
            out.writeString(number);
        }
    
        public static final Parcelable.Creator<MyObject> CREATOR = new Parcelable.Creator<MyObject>() {
            public MyObject createFromParcel(Parcel in) {
                return new MyObject(in);
            }
    
            public MyObject[] newArray(int size) {
                return new MyObject[size];
            }
        };
    }
    

    Save / Restore States

    public class Example extends ListActivity {
        ArrayList<MyObject> list;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            if(savedInstanceState == null || !savedInstanceState.containsKey("key")) {
                String[] colors = {"black", "red", "orange", "cyan", "green", "yellow", "blue", "purple", "magenta", "white"};
                String[] numbers = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
    
                list = new ArrayList<MyObject>();
                for(int i = 0; i < numbers.length; i++) 
                    list.add(new MyObject(numbers[i], colors[i]));
            }
            else {
                list = savedInstanceState.getParcelableArrayList("key");
            }
    
            setListAdapter(new ArrayAdapter<MyObject>(this, android.R.layout.simple_list_item_1, list));
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            outState.putParcelableArrayList("key", list);
            super.onSaveInstanceState(outState);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ArrayList of objects of my custom class. I would like to
I have created an arraylist that is made up of custom objects. Basically the
I have a ArrayList with custom objects. I want to search inside this ArrayList
I have an ArrayList of HashMap like this: ArrayList<HashMap<String, String>> playListSongs = new ArrayList<HashMap<String,
I have an ArrayList of Routine objects and I must use this method to
I have an ArrayList of custom objects. I want to remove duplicate entries. The
I have 2 ArrayList of custom objects. I want to remove duplicate entries from
So I have a custom ArrayAdapter so I can use the Title/Subtitle view that
I have an ArrayList of objects that I need to sort in two different
I have an ArrayList of custom beans. I'd like to use the jstl join

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.