Results 1 to 1 of 1
  1. #1
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default Apply SharedPrefences changes on returning to the activity

    I have a PreferenceActivity with a checkbox and a list of items. Currently, when I navigate from MainActivity to PreferenceActivity and change a setting (e.g. check/uncheck the checkbox), the changes don't affect on returning to the mainActivity immediately and make effects on the app's next run. Since the MainActivity is just Paused and on return hits onStart and onResume, I tried to set a listener for changed preferences on the onResume method of the MainActivity, But it does not work.

    The goal is to show/hide an options menu item (based on checkBox condition), and update the view of listView (not content) to show/hide a view on each row of the listView.

    Java Code:
        
    @Override
        protected void onResume() {
            super.onResume();
            Log.d(ACTIVITY_NAME, "onResume");
    
            // listener is a field
            listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
                @Override
                public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    
                    if (sharedPreferences == preferences && key.contentEquals("favorite")) {
                        invalidateOptionsMenu();
                        //supportInvalidateOptionsMenu();
                        adapter.notifyDataSetChanged();
                        lv.setAdapter(adapter);
    
                    } else if (sharedPreferences == preferences && key.contentEquals("location")) {
                        if (locationPref.contentEquals("gps")) {
                            loadLocation();
                        } else if (locationPref.contentEquals("cityCenter")) {
                            lv.setAdapter(adapter);
                        }
                    }
                }
            };
           preferences.registerOnSharedPreferenceChangeListener(listener);
        }
    I also tried the functional codes above barely and without the listener inside onResume method, in this case show/hide the option menu item is the only thing works.

    Java Code:
        @Override
        protected void onResume() {
            super.onResume();
            Log.d(ACTIVITY_NAME, "onResume");
    
            invalidateOptionsMenu();
            //        supportInvalidateOptionsMenu();
            adapter.notifyDataSetChanged();
    //        lv.setAdapter(adapter);
    
            if (locationPref.contentEquals("gps")) {
                loadLocation();
            } else if (locationPref.contentEquals("cityCenter")) {
                lv.setAdapter(adapter);
            }
    }
    p.s: when I return from PreferenceActivity to the MainActivity the onCreate method isn't called but in case of the other Activity, the onCreate method of the MainActivity is called and this solves many issues (including refreshing the listView, but not only the views bus also the contents)
    Last edited by ghandil; 03-18-2016 at 06:16 PM.

Similar Threads

  1. Replies: 0
    Last Post: 10-01-2011, 04:14 AM
  2. Replies: 1
    Last Post: 09-12-2011, 02:09 AM
  3. How to apply PMD for JSP,HTML
    By Devarajan in forum Advanced Java
    Replies: 8
    Last Post: 01-28-2011, 09:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •