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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:24:41+00:00 2026-05-22T01:24:41+00:00

I am bit confused with how this shared preference works. I had a sample

  • 0

I am bit confused with how this shared preference works. I had a sample code from a website based on shared preferences. My problem is, editor.commit() doesn’t update immediately. Here is my sample code,

 public class PreferencesDemo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Get the app's shared preferences
    SharedPreferences app_preferences = 
        PreferenceManager.getDefaultSharedPreferences(this);

    // Get the value for the run counter
    int counter = app_preferences.getInt("counter", 0);

    // Update the TextView
    TextView text = (TextView) findViewById(R.id.text);
    TextView text1 = (TextView) findViewById(R.id.text1);
    text.setText("This app has been started " + counter + " times.");

    // Increment the counter
    SharedPreferences.Editor editor = app_preferences.edit();
    editor.putInt("counter", counter+2);
  editor.apply();

    editor.commit(); // Very important
    text1.setText("This app has been started " + counter + " times.");
}

}

As you can see, I have counter whose value I am displaying in the first textView and after the commit statement I am printing the updated value in next TextView. But still both the textviews prints the same default value as ‘0’. So if I restart the app, both the textviews are updated.
How to solve this problem. Any help is appreciated.

  • 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-22T01:24:42+00:00Added an answer on May 22, 2026 at 1:24 am

    try this
    for store value in sharePreferences..

    SharedPreferences prefs = getSharedPreferences("Share", Context.MODE_PRIVATE );
    Editor editor = prefs.edit();
    editor.putInt("Value", 1 );
    editor.commit();
    

    for get value

    prefs.getInt("Value",0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a bit confused why this code compiles. I leave out the necessary
Bit confused with this one so bear with me... I have a Navigation-based project
I am a bit confused about this piece of code. public static void Foo(A
I'm a bit confused why this piece of code produces 2 records in a
I am a bit confused about this. If you're building a distributed application, which
I'm a bit confused on this. I have a data table structured like this:
This is a bit confused question for those who don't now advanced SQL... However.
I'm a bit confused as to why this isn't working; I assume that because
I am getting myself a bit confused about how to go about this. My
I don't think this question has been asked before. I'm a bit confused on

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.