I have a PreferenceActivity in which I would like to have the option to change/lock the screen orientation.
The orientation should lock only the Main Activity screen orientation
I not sure were and how I should call the change listener. On the onCreate and onResume of the Main Activity seems right but I can't get it to work.
settings.xml
<ListPreference android:title="Screen orientation"
android:summary="current: %s"
android:key="rotation"
android:defaultValue="1"
android:entries="@array/listArray"
android:entryValues="@array/listValues" />
array.xml
<resources>
<string-array name="listArray">
<item>Auto rotate</item>
<item>Portrait</item>
<item>Landscape</item>
</string-array>
<string-array name="listValues">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
The Preference Activity
public class MyPreferencesActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Links I already looked
How to create a Listener to Preferences changes in Preferences activity?
Adding listeners to individual ListPreference items
How to listen for preference changes within a PreferenceFragment?
OnSharedPreferenceChangeListener not registering change in preference