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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:02:54+00:00 2026-06-02T00:02:54+00:00

I am using SharedPreferences to manage a sort of a session for the user

  • 0

I am using SharedPreferences to manage a sort of a “session” for the user and keeping them logged in until they explicitly press log out which is when I delete everything from the SharedPreferences.

When a user logs in, I do this:

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( LoginActivity.this);

            prefs.edit()
            .putString("first_name", firstName)
            .putString("last_name", lastName)
            .putString("email", email)              
            .putString("user_id", user_id)
            .commit();

Its been working 90% of the time, but every so often, this stuff does not get written to SharedPreferences, causing the user to never be seen as logged in by the system.

Any idea why that might happen? Is it a security issue on some phones?

A note: I put these values into the SystemPreferences when a remote server responds after actually adding the data to the database, and even though the data is added to the db, the values aren’t being saved on some devices.

This is the code to get the preferences:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( ProblemioActivity.this);

String firstName = prefs.getString( "first_name", null); // First arg is name and second is if not found.
String lastName = prefs.getString( "last_name", null); // First arg is name and second is if not found.
String email = prefs.getString( "email", null); // First arg is name and second is if not found.
String user_id = prefs.getString( "user_id", null ); // First arg is name and second is if not found.

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-02T00:02:55+00:00Added an answer on June 2, 2026 at 12:02 am

    Its fine to use SharedPreferences for storing temporary data (like session). But instead of using commit you should use apply, because commit only saves the changes but doesn’t update the already initialized preference object. And when you use apply, it updates the values of preference object and asynchronously saves (commit) the changes too.

    commit()

    Commit your preferences changes back from this Editor to the
    SharedPreferences object it is editing. This atomically performs the
    requested modifications, replacing whatever is currently in the
    SharedPreferences.

    Note that when two editors are modifying preferences at the same time,
    the last one to call commit wins.

    If you don’t care about the return value and you’re using this from
    your application’s main thread, consider using apply() instead.

    .

    apply()

    Commit your preferences changes back from this Editor to the
    SharedPreferences object it is editing. This atomically performs
    the requested modifications, replacing whatever is currently in the
    SharedPreferences.

    Note that when two editors are modifying preferences at the same time,
    the last one to call apply wins.

    Unlike commit(), which writes its preferences out to persistent
    storage synchronously, apply() commits its changes to the in-memory
    SharedPreferences immediately but starts an asynchronous commit to
    disk and you won’t be notified of any failures. If another editor on
    this SharedPreferences does a regular commit() while a apply() is
    still outstanding, the commit() will block until all async commits are
    completed as well as the commit itself.

    As SharedPreferences instances are singletons within a process, it’s
    safe to replace any instance of commit() with apply() if you were
    already ignoring the return value.

    You don’t need to worry about Android component lifecycles and their
    interaction with apply() writing to disk. The framework makes sure
    in-flight disk writes from apply() complete before switching states.

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

Sidebar

Related Questions

I'm using SharedPreferences in my application to store the last User id who logged
I'm using SharedPreferences to persists Object fields. Take this example: class Item { private
My app has the ability to backup and restore SharedPreferences using an BackupAgentHelper class.
What are the disadvantages of using too much SharedPreferences on android?
I am using a PreferenceActivity to let the user set some values. I am
I'm using SharedPreferences to store some app settings. If I push a new version
I understand how to save an application's state by using SharedPreferences, onSavedInstanceState() & onRestoreInstanceState(),
Is it possible to store a Date object using SharedPreferences ? Actually in my
I am using this code: SharedPreferences prefName; prefName = getSharedPreferences(PREF_NAME, MODE_PRIVATE); CharSequence c =
i am using SharedPreferences in my code that will store last state of my

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.