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

  • Home
  • SEARCH
  • 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 6247899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:57:33+00:00 2026-05-24T12:57:33+00:00

I have an ArrayList with custom objects. Each custom object contains a variety of

  • 0

I have an ArrayList with custom objects. Each custom object contains a variety of strings and numbers. I need the array to stick around even if the user leaves the activity and then wants to come back at a later time, however I don’t need the array available after the application has been closed completely. I save a lot of other objects this way by using the SharedPreferences but I can’t figure out how to save my entire array this way. Is this possible? Maybe SharedPreferences isn’t the way to go about this? Is there a simpler method?

  • 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-24T12:57:33+00:00Added an answer on May 24, 2026 at 12:57 pm

    After API 11 the SharedPreferences Editor accepts Sets. You could convert your List into a HashSet or something similar and store it like that. When you read it back, convert it into an ArrayList, sort it if needed and you’re good to go.

    //Retrieve the values
    Set<String> set = myScores.getStringSet("key", null);
    
    //Set the values
    Set<String> set = new HashSet<String>();
    set.addAll(listOfExistingScores);
    scoreEditor.putStringSet("key", set);
    scoreEditor.commit();
    

    You can also serialize your ArrayList and then save/read it to/from SharedPreferences. Below is the solution:

    EDIT:
    Ok, below is the solution to save ArrayList as a serialized object to SharedPreferences and then read it from SharedPreferences.

    Because API supports only storing and retrieving of strings to/from SharedPreferences (after API 11, it’s simpler), we have to serialize and de-serialize the ArrayList object which has the list of tasks into a string.

    In the addTask() method of the TaskManagerApplication class, we have to get the instance of the shared preference and then store the serialized ArrayList using the putString() method:

    public void addTask(Task t) {
      if (null == currentTasks) {
        currentTasks = new ArrayList<task>();
      }
      currentTasks.add(t);
     
      // save the task list to preference
      SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
      Editor editor = prefs.edit();
      try {
        editor.putString(TASKS, ObjectSerializer.serialize(currentTasks));
      } catch (IOException e) {
        e.printStackTrace();
      }
      editor.commit();
    }
    

    Similarly we have to retrieve the list of tasks from the preference in the onCreate() method:

    public void onCreate() {
      super.onCreate();
      if (null == currentTasks) {
        currentTasks = new ArrayList<task>();
      }
     
      // load tasks from preference
      SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_FILE, Context.MODE_PRIVATE);
     
      try {
        currentTasks = (ArrayList<task>) ObjectSerializer.deserialize(prefs.getString(TASKS, ObjectSerializer.serialize(new ArrayList<task>())));
      } catch (IOException e) {
        e.printStackTrace();
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      }
    }
    

    You can get the ObjectSerializer class from the Apache Pig project ObjectSerializer.java

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an arraylist that contains items called Room. Each Room has a roomtype
I have an ArrayList of custom, simple Serializable objects I would like to cache
I was trying to serialize an ArrayList which contains custom objects. I'm serializing it
I have an ArrayList of objects that I need to sort in two different
I have created an arraylist that is made up of custom objects. Basically the
I have an ArrayList of custom objects. I want to remove duplicate entries. The
I have made a custom listView which which extends ListActivity and each row contains
I have created an ArrayList (Java) of my custom class objects with a size
I have an ArrayList<String> , and I want to remove repeated strings from it.
I need to make an ArrayList of ArrayLists thread safe. I also cannot have

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.