我正在尝试使用PreferenceFragmentCompat实现“设置”屏幕。我的偏好xml有一个像这样的首选子屏幕:
的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>
现在,在app中,在我在父活动中实现PreferenceFragmentCompat.OnPreferenceStartScreenCallback接口之前,子屏幕才会打开,如PreferenceFragmentCompat doc中所述。
MainActivity.java
public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat,
PreferenceScreen preferenceScreen) {
preferenceFragmentCompat.setPreferenceScreen(preferenceScreen);
return true;
}
问题出现在哪里。在实现界面时,子屏幕打开,但是我无法找到回到第一个屏幕。
按后退键可关闭应用程序。
有没有什么方法可以在应用栏上放一个后箭头,这样按下它就会把主屏幕拉回来?
答案 0 :(得分:15)
通过使用 setPreferenceScreen ,您将根首选项屏幕设置为子首选项屏幕,这会阻止您使用首选项屏幕层次结构进行导航。
我建议您将每个PreferenceScreen视为片段,并在导航到子屏幕时添加新的片段。
@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));
}
}
当你按下后退按钮时,每个片段将从堆栈中弹出。
有关详细信息,请参阅此GitHub项目
答案 1 :(得分:2)
我添加了一个完整的工作示例,其中包含屏幕截图和代码段here in this post,我希望这会对某人有所帮助。
它包括以下场景: - 1)主设置屏幕,带有两个复选框和一个子屏幕标题。 2)单击子屏幕标题,将打开新的首选项子屏幕。 3)在按下后,控制进入主设置屏幕。因此,正确处理背压。
MainActivity看起来像这样(覆盖onPreferenceStartScreen方法在新窗口中处理新子屏幕的打开): -
public class MainActivity extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceStartScreenCallback {
private static final String TAG = MainActivity.class.getName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = null;
if (savedInstanceState == null) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragment = new AdvancedSettingsFragment().newInstance("Advanced Setting");
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
}
@Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat preferenceFragmentCompat,
PreferenceScreen preferenceScreen) {
Log.d(TAG, "callback called to attach the preference sub screen");
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
AdvancedSettingsSubScreenFragment fragment = AdvancedSettingsSubScreenFragment.newInstance("Advanced Settings Subscreen");
Bundle args = new Bundle();
//Defining the sub screen as new root for the subscreen
args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.getKey());
fragment.setArguments(args);
ft.replace(R.id.fragment_container, fragment, preferenceScreen.getKey());
ft.addToBackStack(null);
ft.commit();
return true;
}
最后是子屏幕片段setPreferencesFromResource(R.xml.preferences, rootKey)
;处理子屏幕与rootkey的连接。
public class AdvancedSettingsSubScreenFragment extends PreferenceFragmentCompat {
private static final String TAG = AdvancedSettingsSubScreenFragment.class.getName();
public static final String PAGE_ID = "page_id";
public static AdvancedSettingsSubScreenFragment newInstance(String pageId) {
AdvancedSettingsSubScreenFragment f = new AdvancedSettingsSubScreenFragment();
Bundle args = new Bundle();
args.putString(PAGE_ID, pageId);
f.setArguments(args);
return (f);
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// rootKey is the name of preference sub screen key name , here--customPrefKey
setPreferencesFromResource(R.xml.preferences, rootKey);
Log.d(TAG, "onCreatePreferences of the sub screen " + rootKey);
}
}