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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:11:28+00:00 2026-06-17T20:11:28+00:00

I would like to store class object in android sharedpreference. I did some basic

  • 0

I would like to store class object in android sharedpreference. I did some basic search on that and I got some answers like make it serializable object and store it but my need is so simple. I would like to store some user info like name, address, age and boolean value is active. I made one user class for that.

public class User {
    private String  name;
    private String address;
    private int     age;
    private boolean isActive;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }
}

Thanks.

  • 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-17T20:11:29+00:00Added an answer on June 17, 2026 at 8:11 pm
    1. Download gson-1.7.1.jar from this link: GsonLibJar

    2. Add this library to your android project and configure build path.

    3. Add the following class to your package.

      package com.abhan.objectinpreference;
      
      import java.lang.reflect.Type;
      import android.content.Context;
      import android.content.SharedPreferences;
      import com.google.gson.Gson;
      import com.google.gson.reflect.TypeToken;
      
      public class ComplexPreferences {
          private static ComplexPreferences       complexPreferences;
          private final Context                   context;
          private final SharedPreferences         preferences;
          private final SharedPreferences.Editor  editor;
          private static Gson                     GSON            = new Gson();
          Type                                    typeOfObject    = new TypeToken<Object>(){}
                                                                      .getType();
      
      private ComplexPreferences(Context context, String namePreferences, int mode) {
          this.context = context;
          if (namePreferences == null || namePreferences.equals("")) {
              namePreferences = "abhan";
          }
          preferences = context.getSharedPreferences(namePreferences, mode);
          editor = preferences.edit();
      }
      
      public static ComplexPreferences getComplexPreferences(Context context,
              String namePreferences, int mode) {
          if (complexPreferences == null) {
              complexPreferences = new ComplexPreferences(context,
                      namePreferences, mode);
          }
          return complexPreferences;
      }
      
      public void putObject(String key, Object object) {
          if (object == null) {
              throw new IllegalArgumentException("Object is null");
          }
          if (key.equals("") || key == null) {
              throw new IllegalArgumentException("Key is empty or null");
          }
          editor.putString(key, GSON.toJson(object));
      }
      
      public void commit() {
          editor.commit();
      }
      
      public <T> T getObject(String key, Class<T> a) {
          String gson = preferences.getString(key, null);
          if (gson == null) {
              return null;
          }
          else {
              try {
                  return GSON.fromJson(gson, a);
              }
              catch (Exception e) {
                  throw new IllegalArgumentException("Object stored with key "
                          + key + " is instance of other class");
              }
          }
      } }
      
    4. Create one more class by extending Application class like this

      package com.abhan.objectinpreference;
      
      import android.app.Application;
      
      public class ObjectPreference extends Application {
          private static final String TAG = "ObjectPreference";
          private ComplexPreferences complexPrefenreces = null;
      
      @Override
      public void onCreate() {
          super.onCreate();
          complexPrefenreces = ComplexPreferences.getComplexPreferences(getBaseContext(), "abhan", MODE_PRIVATE);
          android.util.Log.i(TAG, "Preference Created.");
      }
      
      public ComplexPreferences getComplexPreference() {
          if(complexPrefenreces != null) {
              return complexPrefenreces;
          }
          return null;
      } }
      
    5. Add that application class in your manifest’s application tag like this.

      <application android:name=".ObjectPreference"
          android:allowBackup="false"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" > 
      ....your activities and the rest goes here
      </application>
      
    6. In Your Main Activity where you wanted to store value in Shared Preference do something like this.

      package com.abhan.objectinpreference;
      
      import android.app.Activity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.view.View;
      
      public class MainActivity extends Activity {
          private final String TAG = "MainActivity";
          private ObjectPreference objectPreference;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          objectPreference = (ObjectPreference) this.getApplication();
      
          User user = new User();
          user.setName("abhan");
          user.setAddress("Mumbai");
          user.setAge(25);
          user.setActive(true);
      
          User user1 = new User();
          user1.setName("Harry");
          user.setAddress("London");
          user1.setAge(21);
          user1.setActive(false);
      
          ComplexPreferences complexPrefenreces = objectPreference.getComplexPreference();
          if(complexPrefenreces != null) {
              complexPrefenreces.putObject("user", user);
              complexPrefenreces.putObject("user1", user1);
              complexPrefenreces.commit();
          } else {
              android.util.Log.e(TAG, "Preference is null");
          }
      }
      
      }
      
    7. In another activity where you wanted to get the value from Preference do something like this.

      package com.abhan.objectinpreference;
      
      import android.app.Activity;
      import android.os.Bundle;
      
      public class SecondActivity extends Activity {
          private final String TAG = "SecondActivity";
          private ObjectPreference objectPreference;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_second);
      
          objectPreference = (ObjectPreference) this.getApplication();
          ComplexPreferences complexPreferences = objectPreference.getComplexPreference();
      
          android.util.Log.i(TAG, "User");
          User user = complexPreferences.getObject("user", User.class);
          android.util.Log.i(TAG, "Name " + user.getName());
          android.util.Log.i(TAG, "Address " + user.getAddress());
          android.util.Log.i(TAG, "Age " + user.getAge());
          android.util.Log.i(TAG, "isActive " + user.isActive());
          android.util.Log.i(TAG, "User1");
          User user1 = complexPreferences.getObject("user", User.class);
          android.util.Log.i(TAG, "Name " + user1.getName());
          android.util.Log.i(TAG, "Address " + user1.getAddress());
          android.util.Log.i(TAG, "Age " + user1.getAge());
          android.util.Log.i(TAG, "isActive " + user1.isActive());
      }  }
      

    Hope this can help you. In this answer I used your class for the reference ‘User’ so you can better understand. However we can not relay on this method if you opted to store very large objects in preference as we all know that we have limited memory size for each app in data directory so that if you are sure you have only limited data to store in shared preference you can use this alternative.

    Any suggestions on this implement are most welcome.

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

Sidebar

Related Questions

I have a class in silverlight that I would like to store to disk.
I have a Moose class that i would like to store using Apache::Session::File. However,
I have a class that I would like to store specific times in. The
I would like to store a Class object in an NSMutableDictionary and then instantiate
I would like to store pointers to a Base class in a vector ,
I would like to store some information at the request scope when using google
I would like to store a reference to an object as a weak_ptr. In
Hello I would like to store or save an object inside a session, using
I would like to store the x y and z co-ords for some objects
I'd like to store some information about a class as class (static) variables. However,

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.