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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:23:46+00:00 2026-05-24T14:23:46+00:00

I have an Activity which when clicking the menu and a button appearing there,

  • 0

I have an Activity which when clicking the menu and a button appearing there, goes to a PreferenceActivity, and then loads three ListPreferences.
The ListPreference lets the user choose several values to update a remote DB, and I would like that to save those values when the application goes paused for example.

As the ListPreference are in the PreferenceActivity, how can I get those values?
Where should I save the current preferences state, in the Activity or in the PreferenceActivity?

This is what I have done so far in my Activity.java:

[...]
private void updateFromPreferences() {
        Context context = getApplicationContext();
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        callsFrequencyUpdate = Integer.parseInt(prefs.getString(Preferences.CALLS_FREQUENCY_PREF, "0"));
        smsFrequencyUpdate = Integer.parseInt(prefs.getString(Preferences.SMS_FREQUENCY_PREF, "0"));
        locationFrequencyUpdate = Integer.parseInt(prefs.getString(Preferences.LOCATION_FREQUENCY_PREF, "0"));
    }

    private void savePreferences() {
        SharedPreferences activityPreferences = getPreferences(Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = activityPreferences.edit();
        editor.putInt(Preferences.CALLS_FREQUENCY_PREF, callsFrequencyUpdate);
        editor.putInt(Preferences.SMS_FREQUENCY_PREF, smsFrequencyUpdate);
        editor.putInt(Preferences.LOCATION_FREQUENCY_PREF, locationFrequencyUpdate);
        editor.commit();
    }

    @Override
    protected void onPause() {
        super.onPause();
        savePreferences();
    }

And this is my Preferences.java file:

public class Preferences extends PreferenceActivity  {
    public static final String CALLS_FREQUENCY_PREF = "CALLS_FREQUENCY_PREF";
    public static final String SMS_FREQUENCY_PREF = "SMS_FREQUENCY_PREF";
    public static final String LOCATION_FREQUENCY_PREF = "LOCATION_FREQUENCY_PREF";

    SharedPreferences prefs;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

Thanks a lot in advance!

  • 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-24T14:23:47+00:00Added an answer on May 24, 2026 at 2:23 pm

    It seems like it shouldn’t matter where you write and read those values if they’re within sharedPrefs. However if you’re having trouble with it why not just call PreferenceActivity with startActivityForResult() in your main Activity, and within onPause() in your PreferenceActivity simply pass the information into a bundle/intent with intent.putExtras() and send it back to your main Activity with setResult(), finish()? Then do whatever you want with them in your main Activity by pulling the data with intent.getExtras()? Sorry if I missed exactly what you were asking but it’s not very clear.

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

Sidebar

Related Questions

I have a couple of tables which are used to log user activity for
I have created an activity which sends a number of notifications to status bar.
I have an app that has a list as its main activity and then
I have list of items in my activity which I load dynamically, item after
I have 2 models - User and Activity - which are related by a
I have some 34 checkboxes on one Activity which i have to mark and
I have a Gallery view in my application which is working fine. When clicking
I have an Activity which I want to be finished when user rotates the
I have an activity which shows 3 random images from JSON data off the
I have an activity that has a TabHost containing a set of TabSpecs each

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.