0

I've a code to change a duration of animation, at first start of program "preferenze" runs to read sharedpreferences, later, users can change duration of animation by Preferences.class. Seems ok but I can't update preferences, my code:

@Override
    public void onCreate(Bundle savedInstanceState){ 
      super.onCreate(savedInstanceState);

// runs every time app starts

preferenze();

// read shared private void preferenze() {

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        CheckboxPreference = prefs.getBoolean("checkboxPref", true);
        String ListPreference = prefs.getString("listpref", "1500");


public boolean onCreateOptionsMenu(Menu menu) {

menu.add("Impostazioni").setOnMenuItemClickListener(new OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                    //Toast.makeText(getApplicationContext(), "Vai al Racconto... Non attivo", Toast.LENGTH_SHORT).show();
                    //goToPage();
                Intent settingsActivity = new Intent(getBaseContext(),
                        Preferences.class);
        startActivity(settingsActivity);

public void onSharedPreferenceChanged(SharedPreferences prefs,
            String listpref) {
        Toast.makeText(getApplicationContext(), "eseguo changed: "+listpref, Toast.LENGTH_SHORT).show();
          if (fade == 500){
            animazione = R.style.MyCustomTheme1;
        }
        else if (fade == 1000){
            animazione = R.style.MyCustomTheme2;
        }
        else if (fade == 1500){
            animazione = R.style.MyCustomTheme3;
        }
        else if (fade == 2000){
            animazione = R.style.MyCustomTheme4;
        }

    }

Manifest:

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="17" />
Pol Hallen
  • 1,852
  • 6
  • 32
  • 44
  • It would be more easier to guess what's wrong if you can explain a bit more. If you encounter any kind of error, please post error with logcat. But my wild guess would be that you forget to `commit()` your `SharedPreference Editor` – Hein Dec 30 '12 at 12:53
  • Are you sure that `onSharedPreferenceChanged` is called? (Can you see the Toast?). If yes, I think you forget to commit your edited changes to your properties file. Am I right? – yugidroid Dec 30 '12 at 12:53
  • @yougidroid: no... no toast showes and I don't understand. – Pol Hallen Dec 30 '12 at 12:55
  • @HERO: no errors: but when I change (using preferences) the value of duration, old value is always stored. – Pol Hallen Dec 30 '12 at 12:56
  • @PolHallen, Than means your didn't commited you changes. Take a look at my answer bellow please. – yugidroid Dec 30 '12 at 13:00

1 Answers1

0

If your onSharedPreferenceChanged is being called then you forget to commit your edited changes to your properties file. Try to replace you method with the following one:

public void onSharedPreferenceChanged(SharedPreferences prefs,
            String listpref) {
        Toast.makeText(getApplicationContext(), "eseguo changed: "+listpref, Toast.LENGTH_SHORT).show();
          if (fade == 500){
            animazione = R.style.MyCustomTheme1;
        }
        else if (fade == 1000){
            animazione = R.style.MyCustomTheme2;
        }
        else if (fade == 1500){
            animazione = R.style.MyCustomTheme3;
        }
        else if (fade == 2000){
            animazione = R.style.MyCustomTheme4;
        }

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("listpref", fade);
        editor.commit();
    }

IDK if this your point, but you should follow the kind of ideia. After the change always commit edited values. Let me know if it works.

yugidroid
  • 6,640
  • 2
  • 30
  • 45
  • This might be irrelevant question but How costly is it to retrieve/update shared preferences data? Does the whole process take more than 16 milli seconds (from real time games' perspective)? – BLOB Dec 30 '12 at 13:02
  • I've never thought about it before but it is suposed to be a fast process. Why do you ask? – yugidroid Dec 30 '12 at 13:05
  • @yugidroid For my upcoming game.. being a noob coder, I am using sharedPreferences editor for every 1 second.. anyway.. thanx – BLOB Dec 30 '12 at 13:07
  • What's the target API of your app? Take a look in http://stackoverflow.com/a/3104265/828728 to see if it works. – yugidroid Dec 30 '12 at 13:09
  • Api 17. Changed value by preferences, when I restart app the value is correct. The problem in within app. – Pol Hallen Dec 30 '12 at 13:13