3

I have this preferences class (below) that saves two ListPreferences, but if the ListPreferences are changed and the back button is pressed, the changes don't take affect unless the application is restarted. Did I miss something? Have been looking everywhere, but just can't seem to find an answer the fits or works. Please help.

    public class Preferences extends PreferenceActivity {

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

      @Override
      public void onPause() {
           super.onPause();
           }

      @Override
      public void onResume() {
           super.onResume();
           }
      }

Application Code

 public class Quotes extends Activity implements OnClickListener {

 ProgressDialog dialog;
 private WebView webview;

 @Override
 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

      String q = SP.getString("appViewType","http://www.google.com");
      String c = SP.getString("appRefreshRate","20");

      webview = (WebView) findViewById(R.id.scroll);
      webview.getSettings().setJavaScriptEnabled(true);
      webview.setWebViewClient(new QuotesWebView(this));
      webview.loadUrl(q);

      ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
      timer.scheduleAtFixedRate(new Runnable() {

      @Override
      public void run() {
           webview.reload();
           }

      }, 10, Long.parseLong(c),TimeUnit.SECONDS);

      findViewById(R.id.refresh).setOnClickListener(this);
 }

      @Override
      public void onPause(){
           super.onPause();
           }

      @Override
      public void onResume(){
           super.onResume();
           }

      public void onClick(View v){
           switch(v.getId()){
                case R.id.refresh:
                webview.reload();
           break;
      }
 }


 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);

      MenuItem about = menu.getItem(0);
      about.setIntent(new Intent(this, About.class));

      MenuItem preferences = menu.getItem(1);
      preferences.setIntent(new Intent(this, Preferences.class));

      return true;

      }

 }   
Chris
  • 85
  • 15
  • What do you mean by "the changes don't take effect"? Complete guess:- Are you invalidating your list adapter so that it reloads? – Blundell Jul 03 '11 at 17:20
  • For example. The list will allow to view the data in an image or as text. When the image is displayed and you change the preferences to text, the change will not take effect until you exit the application and reenter. Same with the refresh option. The data can be refreshed automatically every minute or every hour. Again, the changes will not take effect until the application is closed and reopened. – Chris Jul 03 '11 at 17:51

6 Answers6

2

Try overriding the onBackPressed() method.

If your "Up" button (top left <-) provides the correct result, then you can set the Back button to behave like the Up button.

@Override
public void onBackPressed() {
    super.onBackPressed();
    NavUtils.navigateUpFromSameTask(this);
}
borb183
  • 53
  • 7
2

You need to somehow reload your preferences when the preferences activity finishes. I thought Dirol's suggestion of loading them in onResume() instead of onCreate() was excellent; have you tried it? Or am I misunderstanding the problem as well.

In my own case, I launched the preferences activity with startActivityForResult() and then on the activity result callback, I reloaded the preferences.

Code snippets:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case MENU_PREFERENCES:
        Intent intent = new Intent().setClass(this, CalcPreferences.class);
        startActivityForResult(intent, MENU_PREFERENCES);
        break;
      default: return super.onOptionsItemSelected(item);
    }
    return true;
}

@Override
protected void onActivityResult(int req, int result, Intent data) {
    switch( req ) {
      case MENU_PREFERENCES:
        SharedPreferences sp =
          PreferenceManager.getDefaultSharedPreferences(this);
        updatePreferences(sp);
        break;
      default:
        super.onActivityResult(req, result, data);
        break;
    }
}

@Override
protected void updatePreferences(SharedPreferences sp) {
    super.updatePreferences(sp);
    keyclick = sp.getBoolean("keyclick", keyclick);
}

Anyway, this is what works for me. I may try moving my updatePreferences() call to onResume() myself to see if that works too.

Edward Falk
  • 9,991
  • 11
  • 77
  • 112
  • Oh man. I couldn't get the code to work. Would love to do the onResume but be Darned if I can figure out how. I don't have the experience and the class I took was useless. – Chris Jul 03 '11 at 20:00
  • BTW: If anyone could put up the code, a LOT of people would be very grateful. I have been searching for the answer for days and have found tons of folks with the same problem. And usually all anyone gets is the tutorial answer of the onCreate with no onPause or onResume and are told that this should work. – Chris Jul 03 '11 at 20:10
  • OK, let me see if I have your question right: you have an application that launches a preferences activity. You've shown us the code to the preferences activity, but not your application. I can tell you that the code for your preferences application is perfect, although you can delete onPause() and onResume() if you like, since they don't do anything. Your problem is that the changes you make to your preferences don't take effect until you exit and restart your application, correct? – Edward Falk Jul 03 '11 at 23:10
  • Without seeing the code to your application, I'm guessing that somewhere in your onCreate() method, you're loading your preferences. The problem you're having is that once a preferences activity changes your preferences, you have to load them *again*. The code snippets I gave you are for your main application. The first method is the one that launches your preferences activity. The second method is the one that gets notified when the preferences activity exits. This is where you re-load your preferences in order to pick up the changes. Hope this helps. – Edward Falk Jul 03 '11 at 23:12
1

I had the same problem and solved it as follows:

The main activity class implements OnSharedPreferenceChangeListener:

public class Activity_name extends Activity implements OnSharedPreferenceChangeListener  {
    ...
}

Inside the main activity class the onSharedPreferenceChanged is run whenever a preference entry changes. I simply update all my variables from the preferences as i did in onCreate:

@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    <read all preferences as you did in onCreate()>
}

This does the trick and I hope it saves you some time in searching for a solution.

Sammy
  • 51
  • 1
1

You load preferences only on onCreate() method. That method called only when a fresh activity starts up. The addPreferencesFromResource inflates the xml file into the preferences, so you only get the info, which is already has been stored in the xml at the moment addPreferencesFromResource was called, not after.

Try to move that method to onResume. But watch for the memory leak. I don't know exactly what the addPreferencesFromResource do, but from the documentation - I would be very suspicious about that method activity.

Dirol
  • 374
  • 2
  • 11
  • I haven't had much luck trying this. But maybe I wrote it incorrectly. How would that code look exactly? – Chris Jul 03 '11 at 18:19
  • Try to add onStop(){ this.finish();} That should tell the Android, that you need the fresh activity each time it has been moved from the screen. I don't think it is possible to reinflate the XML once again atop of existing inflation, using the API. So we just explicitly closing the activity, so the onCreate method would be called each time. – Dirol Jul 03 '11 at 18:34
  • Sorry, I've misunderstood the question. I thought you need to update the PreferencesActivity itself from the updated XML. It seems you'd like to apply preferences at the time - they've been selected. The problem in that case is: in the place, where you apply the preferences for the application, after the changes, you need to call your reload preferences method. The PreferenceActivity only saves the new preferences in the file. And it's up to you - to read the values from that file - and to apply them when needed. – Dirol Jul 03 '11 at 18:49
  • It seems - you have that applying only at the beggining of the application (maybe at another onCreate or onStart method for the main Activity), and you're not recalling it after the changes has been made. – Dirol Jul 03 '11 at 18:49
0

I've had the same problem... Try to create preference instance and load its data in every class and every activity where you need it. It worked for me...Hope it helps.

Veljko
  • 1,893
  • 6
  • 28
  • 58
0

You will need to reload your view or whatever object which uses those preferences, preferably when preference activity closes.

Preference activities do not change nothing but an internal file with your preferences(key=value list). When it is changed, preferenceActivity calls onPreferenceChaged() and nothing more. It doesn't refresh your stuff by itself. You need to reload prefs and to reuse them in onResume() method or equivalent.

durron597
  • 31,968
  • 17
  • 99
  • 158