samedi 27 juin 2015

How to Update a Fragment From PreferenceActivity?

My main activity has several fragments, one of which has a listview or gridview. I also have a preferenceActivity that configures how the app looks and functions, in particular whether to display the list or the grid.

When the list/grid view setting is changed in the preference activity I want to reflect that change in my fragment (located in my app's main activity). How do I do so from a separate activity? (in this cast from the preference activity).

Edit- I managed to accomplish this by using the onResume() callback to check if any relevant preference changed, and if there was a change:

public void onResume() {
    super.onResume();
    SharedPreferences settings = getActivity().getSharedPreferences("settings", 0);
    boolean layout = settings.getBoolean("layout", false);
    if (layout == true) {
        listview.setVisibility(View.VISIBLE);
        gridview.setVisibility(View.GONE);
    } else {
        listview.setVisibility(View.GONE);
        gridview.setVisibility(View.VISIBLE);
    }

}

Aucun commentaire:

Enregistrer un commentaire