0

I'm using a SharedPreference to store a zoom value for a camera preview. From the SharedPreference menu, if I change the zoom value from there everything works fine. I've also implemented a ScaleGestureDetector for setting this zoom value.

The problem is, when using the gesture detector, when I go into the SharedPreference menu it has not updated the control.

The actual XML for the SharedPreference (PrefsFile.xml) has updated -

<float name="Zoom Area" value="1.0" />

However the XML file for the UI part (package_preference.xml) is still set to the previous value -

<string name="pref_zoom_area">0.7</string>

My listener is inside a fragment -

public class Camera2VideoFragment extends Fragment implements View.OnClickListener, FragmentCompat.OnRequestPermissionsResultCallback, View.OnTouchListener {

    ...

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor();

            // Don't let the object get too small or too large.
            mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 1.0f));

            return true;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            mParameters.set_zoom_area(mScaleFactor);
            SharedPreferences settings = getActivity().getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
            SharedPreferences.Editor editor = settings.edit();
            Log.d(TAG, "onScaleEnd() mParameters.get_zoom_area() = " + mParameters.get_zoom_area());
            editor.putFloat(getString(R.string.pref_zoom_area), mParameters.get_zoom_area());
            editor.apply();
        }
    }

    ...
}

I can confirm onScaleEnd() is being called.

I previous had editor.commit() but I thought editor.apply() would have fixed this issue but is hasn't.

jimbo
  • 416
  • 1
  • 4
  • 14
  • 1
    Does [this question](https://stackoverflow.com/questions/5652682/android-preferences-what-is-the-difference) shed light on your issue? – Haem Oct 19 '17 at 10:12

1 Answers1

0

So I have this working now. I've created a method to update the preference value when the respective fragment that displays the preference view is fully available at onResume().

@Override
public void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
    updatePreferencesUI();
}

public void updatePreferencesUI() {
    ListPreference mListPreference;

    // Some settings may have been changed from outside this fragment. Even though the
    // SharedPreference itself may be updated, the UI controls for this fragment will need
    // updating separately, otherwise the two groups would be out of sync.
    CameraActivity cameraActivity = (CameraActivity) getActivity();

    mListPreference = (ListPreference) findPreference("pref_zoom_area");
    mListPreference.setValue(String.valueOf(cameraActivity.mParameters.get_zoom_area()));

}
jimbo
  • 416
  • 1
  • 4
  • 14