0

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);
    }

}
Whome
  • 507
  • 1
  • 8
  • 24
  • 1
    use onResume of the fragment to check the preference values – tyczj Jun 25 '15 at 13:19
  • I hope this link may be help you http://stackoverflow.com/questions/13596250/how-to-listen-for-preference-changes-within-a-preferencefragment – Vatsal Shah Jun 25 '15 at 13:20

2 Answers2

0

You can start the PreferenceActivity by using the startActivityForResult which return the result in the MainActivity.In the onActivityForResult() update the view.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK){
            //Update the view.
        }
}//onActivityResult
Kartheek
  • 7,104
  • 3
  • 30
  • 44
0

you should create a interface which listen the change action from preference activity and then main activity / fragment must implement interface above.

ThaiPD
  • 3,503
  • 3
  • 30
  • 48