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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:47:31+00:00 2026-06-02T03:47:31+00:00

I am trying to update the values of SharedPreferences , here is my code:

  • 0

I am trying to update the values of SharedPreferences, here is my code:

edit = PreferenceManager.getDefaultSharedPreferences(this).edit();
edit.putString(Settings.PREF_USERNAME+"",txtuser);
edit.putString(Settings.PREF_PASSWORD+"",txtpass);
edit.commit();" 

The problem is that when I am accessing this values, it is not returning updated values, it gives me a value of SharedPreferences.

But when I am confirming the data in XML file ,the data updated in that.

And after restarting my application I am getting that updated values. So it requires me to restart the application to get updated values.
So, how to get those updated values once it changes?

Thanks in advance

Here is my whole code:

@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        ctx=this;

            status=PreferenceManager.getDefaultSharedPreferences(this).getString(Settings.PREF_STATUS, Settings.DEFAULT_STATUS);// get old value
        submit.setOnClickListener(new View.OnClickListener() {
          @Override
            public void onClick(View v) {

                  on(ctx,true);//  function will call and value is updated

                }
            }});    

     status=PreferenceManager.getDefaultSharedPreferences(this).getString(Settings.PREF_STATUS, Settings.DEFAULT_STATUS);// this should give me a updated value but gives old value

    }
    public static boolean on(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Settings.PREF_ON, Settings.DEFAULT_ON);
    }

    public static void on(Context context,boolean on) {
            if (on) Receiver.engine(context).isRegistered(); //
        }




**********in reciver file***********
public void isRegistered ) {
        Editor edit = PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).edit();
        edit.putString(Settings.PREF_STATUS+"","0");
        edit.commit();
}
  • 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-02T03:47:34+00:00Added an answer on June 2, 2026 at 3:47 am

    Instead of using edit.commit();, you should use edit.apply();. Apply will update the preference object instantly and will save the new values asynchronously, so allowing you to read the latest values.


    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()

    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 trying to update/change contact ringtone using this code: ContentValues values = new ContentValues();
I am trying to update values in a column like the following code block
I am trying update text views while this loop is processing. Here is my
i'm trying to update my code with values that already exist there(in the same
this code throw exception while i am trying to update value ,first value only
I'm trying to update one table based on values in another table. What's wrong
i'm trying to make a ajax update in prototype with some values from a
Hit a speed bump, trying to update some column values in my table from
I am trying to update only some values of an Array directly. Which is
I am using https://github.com/mranney/node_redis and trying to update a set with multiple values at

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.