单击“首选项”时未调用“ onPreferenceStartFragment()”方法

时间:2019-07-24 19:11:52

标签: java android android-studio android-fragments androidx

迁移到AndroidX时,我有一个“偏好设置”屏幕,用户可以选择不同的偏好设置。最终目的是,当用户单击首选项时,会将他们带到新屏幕。新屏幕将根据Google倡导的内容扩展PreferenceFragmentCompat。将屏幕链接在一起,我在XML中声明了“ app:fragment”。

当用户点击带有相关片段的首选项时,界面方法 会调用PreferenceFragmentCompat.OnPreferencesStartFragmentCallback.onPreferenceStartFragment()。此方法是您处理新屏幕的位置,应在周围的Activity中实现。

onPreferenceStartFragment方法永远不会被调用,并且屏幕永远不会膨胀。我在下面添加了一些代码。

我已经尝试将依赖项添加到gradle中以解决该问题。

'''

preference.xml

    <PreferenceCategory
            android:key="@string/aboutPreferencesCategory"
            android:title="@string/settings_aboutPreferencesCategoryTitle">

            <Preference
                android:id="@+id/about_preference_screen"
                android:key="@string/aboutPreferenceKey"
                android:layout="@layout/preference_main"
                app:fragment="com.fitnesskeeper.****.About****Fragment"
                android:title="@string/settings_aboutTitle"/> 
    '''



SettingsActivity.java extends AppCompatActivity implements PreferenceFragmentCompat.OnPreferenceStartFragmentCallback

     @Override
        public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, androidx.preference.Preference pref) {

            // Instantiate the new Fragment
            final Bundle args = pref.getExtras();
            final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(getClassLoader(), pref.getFragment(),args);
            fragment.setArguments(args);
            fragment.setTargetFragment(caller, 0);

            // Replace the existing Fragment with the new Fragment
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragmentContainer, fragment)
                    .addToBackStack(null)
                    .commit();
            return true;
        }
    } 

'''

AboutFragment.java extends PreferenceFragmentCompat


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.about_runkeeper,container,false);
    return view;
}

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    addPreferencesFromResource(R.xml.preferences);

'''

grade(module app)



      implementation('androidx.appcompat:appcompat:1.0.2')
        implementation ('androidx.preference:preference:1.0.0')
        implementation('androidx.legacy:legacy-support-v4:1.0.0')
        implementation('androidx.legacy:legacy-support-v13:1.0.0')
        implementation('androidx.appcompat:appcompat:1.0.0')
        implementation('androidx.cardview:cardview:1.0.0')
        implementation('androidx.recyclerview:recyclerview:1.0.0')
        implementation('androidx.fragment:fragment:1.1.0-alpha01')
        implementation('com.google.android.material:material:1.0.0')
        implementation('androidx.transition:transition:1.0.0')
        implementation('androidx.browser:browser:1.0.0')
        implementation('androidx.exifinterface:exifinterface:1.0.0')

'''

I have a breakpoint on the onPreferenceStartFragment but the breakpoint is x'd out and not checking off as green. My boss told me, that means the method would never be executed.

1 个答案:

答案 0 :(得分:0)

回调由OnPreferenceTreeClick()处理。如果您在不调用其超级版本的情况下覆盖它,它将无法正常工作。