简体   繁体   English

PreferenceFragment中的PreferenceScreen在ScreenOrientation更改时关闭

[英]PreferenceScreen in PreferenceFragment closing on ScreenOrientation change

I got some nested PreferenceScreen in my PreferenceActivity , and under them a series of CheckBoxPreference . 我得到了一些嵌套PreferenceScreen在我PreferenceActivity ,并根据他们的一系列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. 一切运行良好,但是无论何时旋转设备,PreferenceActivity都会返回到主PreferenceScreen ,而无需考虑用户所在的嵌套首选项屏幕。

This is exactly like in these previous SO questions, where the solution was to add a key to the PreferenceScreen: 这与前面的这些SO问题完全相同,在该问题中,解决方案是向PreferenceScreen添加一个键:

  1. When my PreferenceActivity rotates, it does not remember which PreferenceScreen was open 当我的PreferenceActivity旋转时,它不记得打开了哪个PreferenceScreen

  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 嵌套首选项屏幕在Android中更改屏幕方向时关闭

I've added keys to all my PreferenceScreen and the solution works, as long as I use the deprecated way: 我已将密钥添加到我的所有PreferenceScreen中,并且该解决方案有效,只要我使用不赞成使用的方式即可:

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 ). 问题是我正在使用PreferenceFragment ,就像在这个SO答案中一样 (也在此处 )。

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? 那么,如果可能,使用当前代码,如何在更改屏幕方向后如何保持内部PreferenceScreen打开?

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. 我看到Google 推荐使用 “偏好设置”, 现在推荐 AppCompatActivity和PreferenceFragmentCompat,但是我不愿使用任何库,甚至不使用Google的任何库,尤其是只针对这么小的细节。

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. 当Android在API 11下时,这个小问题设法产生了三个SO问题,而在PreferenceFragment被弃用时,在API 11和API 28之间没有一个(至少可以找到),我对此感到非常惊讶。

Anyways, I found the solution here in SO as well, in an answer to Android- deprecated method warning regarding PreferenceActivity . 无论如何,我也在SO中找到了解决方案,它是针对Android弃用的有关PreferenceActivity的方法警告的答案。

The key is to check if (savedInstanceState == null) before adding the PreferenceFragment , like this: 关键是在添加PreferenceFragment之前检查if (savedInstanceState == null)像这样:

@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. 现在,只要屏幕方向改变,嵌套的PreferenceScreen便会保持打开状态,只要设置了android:key值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM