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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:43:13+00:00 2026-06-11T06:43:13+00:00

I would like to store a Location object and am trying to pick a

  • 0

I would like to store a Location object and am trying to pick a good way to do it. I just have the one small object, and I need it to be private, so SharedPreferences or Internal Storage make the most sense to me so far.

I see that an object can be written to a bytearray, stored as a String in SharedPreferences, then written back to an object as described here.

Is that a reasonable approach? Is there a better way?

Thanks for any advice.

  • 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-11T06:43:14+00:00Added an answer on June 11, 2026 at 6:43 am

    When saving simple data I prefer using JSON objects as the code is simpler and easier to understand for future maintainers than manipulating byte arrays. Since the objects are small the size penalty for using strings instead of byte arrays is not significant.

    private static final String LATITUDE = "com.somepackage.name.LATITUDE";
    private static final String LONGITUDE = "com.somepackage.name.LONGITUDE";
    
    /**
     * Save a location/key pair.
     * 
     * @param key the key associated with the location
     * @param location the location for the key
     * @return true if saved successfully false otherwise
     */
    public boolean saveLocation(String key, Location location) {
        LOG.info("Saving location");
        try {
            JSONObject locationJson = new JSONObject();
    
            locationJson.put(LATITUDE, location.getLatitude());
            locationJson.put(LONGITUDE, location.getLongitude());
            //other location data
            SharedPreferences.Editor edit = preferences.edit();
            edit.putString(key, locationJson.toString());
            edit.commit();
        } catch (JSONException e) {
            LOG.error("JSON Exception", e);
            return false;
        }
    
        LOG.info("Location {}  saved successfully at key: {}", preferences.getString(key, null),key);
        return true;
    }
    
    /**
     * Gets location data for a key.
     * 
     * @param key the key for the saved location
     * @return a {@link Location} object or null if there is no entry for the key
     */
    public Location getLocation(String key) {
        LOG.info("Retrieving location at key {} ", key);
        try {
            String json = preferences.getString(key, null);
    
            if (json != null) {
                JSONObject locationJson = new JSONObject(json);
                Location location = new Location(STORAGE);
                location.setLatitude(locationJson.getInt(LATITUDE));
                location.setLongitude(locationJson.getInt(LONGITUDE));
                LOG.info("Returning location: {}" , location);
                return location;
            }
        } catch (JSONException e) {
            LOG.error("JSON Exception", e);
        }
    
        LOG.warn("No location found at key {}",  key);
        //or throw exception depending on your logic
        return null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have objects with location data stored in Core Data, I would like to
I have a certificate .p12 and .crt and I would like store the public
I would like to store a small profile picture of my users in their
I would like to store the cookies from one open-uri call and pass them
We have a system with many web servers, where we would like to store
I have run into a situation in which I would like to store an
I would like to store all paths to headers in separate file. I'm going
I would like to store some information at the request scope when using google
I would like to store the points whenever touchesMoved is called. So far everything
I would like to store historical equity price data in a table in the

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.