tags:

views:

204

answers:

1

I have a ListView that has some minor visual preferences that are set in a PreferenceScreen. These preferences are simple booleans to show or not to show some specific TextViews on each item in my ListView. Anyhow, when these preferences are changed, I need to notify my ArrayAdapter that the data has changed in order to get the list redrawn. However, doing this via an OnSharedPreferenceChangeListener wouldn't really be optimal because there are several preferences that you can change, that would cause an unnecessary amount of updates to the ArrayAdapter. So, to the question: How can I identify when my ListActivity has occurred on the screen after closing my PreferenceActivity, which I then could use to check for changes in the preferences, and only then notify the ArrayAdapter.

Edit: The ArrayAdapter being an inner class of my ListActivity, which is set as a ListAdapter.

A: 

Hi,
There are several ways of doing it. As you noticed, the good way is to update your ListActivity when it becomes visible again. To achieve that you can:

  1. simply override onResume() method from your ListActivity. Method will be invoked when your activity comes to the foreground.

  2. Another good solution would be to start your PreferenceActivity in such way:

    
    //class member of your list activity
    private static final int CODE_PREFERENCES = 1;
    ...
    //running your preferences from list activity
    Intent preferencesIntent = new Intent().setClass(this, YourPreferences.class);
    startActivityForResult(preferencesIntent, CODE_PREFERENCES);
    

You also have to implement onActivityResult() in your list activity. This method will be invoked when preferences activity has been closed.


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == CODE_PREFERENCES) {
      // notify your adapter that data has changed
        }
    }
Ramps