PreferenceScreen in PreferenceFragment closing on ScreenOrientation change












0















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:




  1. When my PreferenceActivity rotates, it does not remember which
    PreferenceScreen was open


  2. How to prevent quitting from inner preference screen when there's a
    configuration change


  3. Nested 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.










share|improve this question























  • 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


















0















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:




  1. When my PreferenceActivity rotates, it does not remember which
    PreferenceScreen was open


  2. How to prevent quitting from inner preference screen when there's a
    configuration change


  3. Nested 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.










share|improve this question























  • 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
















0












0








0








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:




  1. When my PreferenceActivity rotates, it does not remember which
    PreferenceScreen was open


  2. How to prevent quitting from inner preference screen when there's a
    configuration change


  3. Nested 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.










share|improve this question














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:




  1. When my PreferenceActivity rotates, it does not remember which
    PreferenceScreen was open


  2. How to prevent quitting from inner preference screen when there's a
    configuration change


  3. Nested 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.







android android-preferences screen-orientation preferencefragment preferencescreen






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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



















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














1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 16:04









        RodiaRodia

        1,15971623




        1,15971623
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith