"当点击Preference时,"onPreferenceStartFragment() "方法未被调用。

问题描述 投票:0回答:1

迁移到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.
java android android-studio android-fragments androidx
1个回答
0
投票

回调由OnPreferenceTreeClick()处理。如果你不调用它的超级版本而重写它,它就不会工作。

© www.soinside.com 2019 - 2024. All rights reserved.