问题描述
我正在尝试使用PreferenceFragmentCompat实现设置"屏幕.我的首选项xml具有这样的首选项子屏幕:
I am trying to implement a Settings screen using PreferenceFragmentCompat. My preference xml has a preference subscreen like this:
preferences.xml
<CheckBoxPreference
android:defaultValue="false"
android:key="@string/pref_sound_key"
android:summary="@string/pref_sound_summary"
android:title="@string/pref_sound_title" />
<PreferenceScreen android:title="Inner Screen">
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/key_1"
android:title="@string/title_1" />
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/key_1"
android:title="@string/title_1" />
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/key_2"
android:title="@string/title_2" />
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/key_3"
android:title="@string/title_3" />
</PreferenceScreen>
</PreferenceScreen>
现在,在应用程序中,直到按照PreferenceFragmentCompat文档中的规定在父活动中实现PreferenceFragmentCompat.OnPreferenceStartScreenCallback接口之后,子屏幕才会打开.
Now, in the app, the subscreen does not open until I implement PreferenceFragmentCompat.OnPreferenceStartScreenCallback interface in parent activity, as specified in PreferenceFragmentCompat doc.
MainActivity.java
public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat,
PreferenceScreen preferenceScreen) {
preferenceFragmentCompat.setPreferenceScreen(preferenceScreen);
return true;
}
在这里出现问题.在实现该界面时,将打开子屏幕,但是找不到重新回到第一个屏幕的方法.
Here's where the problem arises. On implementing the interface, the subscreen opens, but then there is no way I can find to move back to first screen.
按返回键将关闭应用程序.
Pressing back key closes the app.
有什么办法可以在应用程序栏上放置后退箭头,以便按下该按钮可以使主屏幕返回?
Is there any way I can put a back arrow on app bar so that pressing it will bring the main screen back?
推荐答案
使用 setPreferenceScreen 将根首选项屏幕设置为子首选项屏幕,这将阻止您使用首选项屏幕的层次结构导航回来.
By using setPreferenceScreen you are setting the root preference screen to the sub preference screen which is preventing you from having a hierarchy of preference screens to navigate back through.
我建议您将每个PreferenceScreen视作一个片段,并在导航到子屏幕时添加一个新的片段.
I suggest that you treat each PreferenceScreen as a Fragment and add a new Fragment when you navigate into a sub screen.
@Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat,
PreferenceScreen preferenceScreen) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
MyPreferenceFragment fragment = new MyPreferenceFragment();
Bundle args = new Bundle();
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.getKey());
fragment.setArguments(args);
ft.add(R.id.fragment_container, fragment, preferenceScreen.getKey());
ft.addToBackStack(preferenceScreen.getKey());
ft.commit();
return true;
}
MyPreferenceFragment
public class MyPreferenceFragment extends AppPreferenceFragment {
public static final String FRAGMENT_TAG = "my_preference_fragment";
public MyPreferenceFragment() {
}
@Override
public void onCreatePreferences(Bundle bundle, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
}
AppPreferenceFragment
public abstract class AppPreferenceFragment extends PreferenceFragmentCompat {
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Set the default white background in the view so as to avoid transparency
view.setBackgroundColor(
ContextCompat.getColor(getContext(), R.color.background_material_light));
}
}
这样,当您按下返回按钮时,每个片段将从堆栈中弹出.
That way when you press the back button each Fragment will be popped from the stack.
有关更多信息,请参见此 GitHub 项目
For more information see this GitHub project
这篇关于如何从PreferenceFragmentCompat中的“首选项"子屏幕移回主屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!