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.