PreferenceScreen in PreferenceFragment closing on ScreenOrientation change
I got some nested PreferenceScreen
in my PreferenceActivity
, and under them a series of CheckBoxPreference
.
Everything is working well but, whenever the device is rotated, the PreferenceActivity returns to the main PreferenceScreen
, disregarding what nested preference screen the user was in.
This is exactly like in these previous SO questions, where the solution was to add a key to the PreferenceScreen:
When my PreferenceActivity rotates, it does not remember which
PreferenceScreen was openHow to prevent quitting from inner preference screen when there's a
configuration changeNested Preference Screen closes on Screenorientation change in
Android
I've added keys to all my PreferenceScreen and the solution works, as long as I use the deprecated way:
public class Settings extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
The problem is I'm using a PreferenceFragment
, like in this SO answer (and also here).
The code:
public class Settings 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.prefs);
}
}
}
So, how to keep inner PreferenceScreen open after screen orientation changes, if possible with the current code?
I see that for Preferences Google now recommends AppCompatActivity and PreferenceFragmentCompat, but I would prefer to not use any libraries, not even Google's, specially for only such a small detail.

add a comment |
I got some nested PreferenceScreen
in my PreferenceActivity
, and under them a series of CheckBoxPreference
.
Everything is working well but, whenever the device is rotated, the PreferenceActivity returns to the main PreferenceScreen
, disregarding what nested preference screen the user was in.
This is exactly like in these previous SO questions, where the solution was to add a key to the PreferenceScreen:
When my PreferenceActivity rotates, it does not remember which
PreferenceScreen was openHow to prevent quitting from inner preference screen when there's a
configuration changeNested Preference Screen closes on Screenorientation change in
Android
I've added keys to all my PreferenceScreen and the solution works, as long as I use the deprecated way:
public class Settings extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
The problem is I'm using a PreferenceFragment
, like in this SO answer (and also here).
The code:
public class Settings 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.prefs);
}
}
}
So, how to keep inner PreferenceScreen open after screen orientation changes, if possible with the current code?
I see that for Preferences Google now recommends AppCompatActivity and PreferenceFragmentCompat, but I would prefer to not use any libraries, not even Google's, specially for only such a small detail.

As what you stated in the last sentence, it's preferred to use an activity which extendsAppCompatActivity
and you can add a fragment which extendsPreferenceFragmentCompat
into the activity's view, as compared to using the older form which used to be in the same docs as to what you linked (but it seems to be updated ever since..).
– Edric
Nov 22 '18 at 13:51
add a comment |
I got some nested PreferenceScreen
in my PreferenceActivity
, and under them a series of CheckBoxPreference
.
Everything is working well but, whenever the device is rotated, the PreferenceActivity returns to the main PreferenceScreen
, disregarding what nested preference screen the user was in.
This is exactly like in these previous SO questions, where the solution was to add a key to the PreferenceScreen:
When my PreferenceActivity rotates, it does not remember which
PreferenceScreen was openHow to prevent quitting from inner preference screen when there's a
configuration changeNested Preference Screen closes on Screenorientation change in
Android
I've added keys to all my PreferenceScreen and the solution works, as long as I use the deprecated way:
public class Settings extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
The problem is I'm using a PreferenceFragment
, like in this SO answer (and also here).
The code:
public class Settings 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.prefs);
}
}
}
So, how to keep inner PreferenceScreen open after screen orientation changes, if possible with the current code?
I see that for Preferences Google now recommends AppCompatActivity and PreferenceFragmentCompat, but I would prefer to not use any libraries, not even Google's, specially for only such a small detail.

I got some nested PreferenceScreen
in my PreferenceActivity
, and under them a series of CheckBoxPreference
.
Everything is working well but, whenever the device is rotated, the PreferenceActivity returns to the main PreferenceScreen
, disregarding what nested preference screen the user was in.
This is exactly like in these previous SO questions, where the solution was to add a key to the PreferenceScreen:
When my PreferenceActivity rotates, it does not remember which
PreferenceScreen was openHow to prevent quitting from inner preference screen when there's a
configuration changeNested Preference Screen closes on Screenorientation change in
Android
I've added keys to all my PreferenceScreen and the solution works, as long as I use the deprecated way:
public class Settings extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
The problem is I'm using a PreferenceFragment
, like in this SO answer (and also here).
The code:
public class Settings 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.prefs);
}
}
}
So, how to keep inner PreferenceScreen open after screen orientation changes, if possible with the current code?
I see that for Preferences Google now recommends AppCompatActivity and PreferenceFragmentCompat, but I would prefer to not use any libraries, not even Google's, specially for only such a small detail.


asked Nov 22 '18 at 13:44


RodiaRodia
1,15971623
1,15971623
As what you stated in the last sentence, it's preferred to use an activity which extendsAppCompatActivity
and you can add a fragment which extendsPreferenceFragmentCompat
into the activity's view, as compared to using the older form which used to be in the same docs as to what you linked (but it seems to be updated ever since..).
– Edric
Nov 22 '18 at 13:51
add a comment |
As what you stated in the last sentence, it's preferred to use an activity which extendsAppCompatActivity
and you can add a fragment which extendsPreferenceFragmentCompat
into the activity's view, as compared to using the older form which used to be in the same docs as to what you linked (but it seems to be updated ever since..).
– Edric
Nov 22 '18 at 13:51
As what you stated in the last sentence, it's preferred to use an activity which extends
AppCompatActivity
and you can add a fragment which extends PreferenceFragmentCompat
into the activity's view, as compared to using the older form which used to be in the same docs as to what you linked (but it seems to be updated ever since..).– Edric
Nov 22 '18 at 13:51
As what you stated in the last sentence, it's preferred to use an activity which extends
AppCompatActivity
and you can add a fragment which extends PreferenceFragmentCompat
into the activity's view, as compared to using the older form which used to be in the same docs as to what you linked (but it seems to be updated ever since..).– Edric
Nov 22 '18 at 13:51
add a comment |
1 Answer
1
active
oldest
votes
I'm quite surprised this little problem managed to generate three SO questions while Android was under API 11, and none - that I can find at least - between API 11 and API 28, when PreferenceFragment
was deprecated.
Anyways, I found the solution here in SO as well, in an answer to Android- deprecated method warning regarding PreferenceActivity.
The key is to check if (savedInstanceState == null)
before adding the PreferenceFragment
, like this:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
getFragmentManager().
beginTransaction().
replace(android.R.id.content, new MyPreferenceFragment()).
commit();
}
Now the nested PreferenceScreen remains open when screen orientation changes, as long as it has an android:key
value set in, of course.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432362%2fpreferencescreen-in-preferencefragment-closing-on-screenorientation-change%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm quite surprised this little problem managed to generate three SO questions while Android was under API 11, and none - that I can find at least - between API 11 and API 28, when PreferenceFragment
was deprecated.
Anyways, I found the solution here in SO as well, in an answer to Android- deprecated method warning regarding PreferenceActivity.
The key is to check if (savedInstanceState == null)
before adding the PreferenceFragment
, like this:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
getFragmentManager().
beginTransaction().
replace(android.R.id.content, new MyPreferenceFragment()).
commit();
}
Now the nested PreferenceScreen remains open when screen orientation changes, as long as it has an android:key
value set in, of course.
add a comment |
I'm quite surprised this little problem managed to generate three SO questions while Android was under API 11, and none - that I can find at least - between API 11 and API 28, when PreferenceFragment
was deprecated.
Anyways, I found the solution here in SO as well, in an answer to Android- deprecated method warning regarding PreferenceActivity.
The key is to check if (savedInstanceState == null)
before adding the PreferenceFragment
, like this:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
getFragmentManager().
beginTransaction().
replace(android.R.id.content, new MyPreferenceFragment()).
commit();
}
Now the nested PreferenceScreen remains open when screen orientation changes, as long as it has an android:key
value set in, of course.
add a comment |
I'm quite surprised this little problem managed to generate three SO questions while Android was under API 11, and none - that I can find at least - between API 11 and API 28, when PreferenceFragment
was deprecated.
Anyways, I found the solution here in SO as well, in an answer to Android- deprecated method warning regarding PreferenceActivity.
The key is to check if (savedInstanceState == null)
before adding the PreferenceFragment
, like this:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
getFragmentManager().
beginTransaction().
replace(android.R.id.content, new MyPreferenceFragment()).
commit();
}
Now the nested PreferenceScreen remains open when screen orientation changes, as long as it has an android:key
value set in, of course.
I'm quite surprised this little problem managed to generate three SO questions while Android was under API 11, and none - that I can find at least - between API 11 and API 28, when PreferenceFragment
was deprecated.
Anyways, I found the solution here in SO as well, in an answer to Android- deprecated method warning regarding PreferenceActivity.
The key is to check if (savedInstanceState == null)
before adding the PreferenceFragment
, like this:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
getFragmentManager().
beginTransaction().
replace(android.R.id.content, new MyPreferenceFragment()).
commit();
}
Now the nested PreferenceScreen remains open when screen orientation changes, as long as it has an android:key
value set in, of course.
answered Nov 22 '18 at 16:04


RodiaRodia
1,15971623
1,15971623
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432362%2fpreferencescreen-in-preferencefragment-closing-on-screenorientation-change%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
As what you stated in the last sentence, it's preferred to use an activity which extends
AppCompatActivity
and you can add a fragment which extendsPreferenceFragmentCompat
into the activity's view, as compared to using the older form which used to be in the same docs as to what you linked (but it seems to be updated ever since..).– Edric
Nov 22 '18 at 13:51