偏好碎片不在同一个碎片管理器中



我们有一个PreferenceFragmentCompat,通过点击首选项,我们希望从当前的PreferenceFragmentCompat切换到新的PreferenceFragmentCompat。(在新屏幕上进行某些设置(。

然而,无论我们尝试了什么,我们都会不断遇到以下错误:

片段声明的目标片段不属于FragmentManager

MainActivity.kt

class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback, PreferenceFragmentCompat.OnPreferenceStartFragmentCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_preferences
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, pref: Preference): Boolean {
val args = pref.extras
val fragment = supportFragmentManager.fragmentFactory.instantiate(
classLoader,
pref.fragment)
fragment.arguments = args
fragment.setTargetFragment(caller, 0)
// Replace the existing Fragment with the new Fragment
supportFragmentManager.beginTransaction()
.replace(R.id.nav_host_fragment, fragment)
.addToBackStack(null)
.commit()
return true
}
}

首选碎片1.kt

class PreferencesFragment1 : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences1, rootKey)
}
}

偏好碎片2.kt

class PreferenceFragment2 : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences2, rootKey)
}
}

首选项1.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<PreferenceCategory
android:title="Category"
app:iconSpaceReserved="false">
<Preference
app:key="pref2"
app:iconSpaceReserved="false"
android:title="Open"
app:fragment="com.testapp.ui.preferences.Preference2"/>
</PreferenceCategory>
</PreferenceScreen>

首选项2.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
</PreferenceScreen>

堆栈跟踪显示以下内容:

java.lang.IllegalStateException: Fragment Preference2{496ebb9} (1b91d8df-58c0-485a-96f9-d392fcef528b) id=0x7f09008d} declared target fragment Preference1{b6337c1} (97aa1b0e-d5fa-4995-b0ba-e89ad0ea5ce0) id=0x7f09008d} that does not belong to this FragmentManager!
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1148)
at androidx.fragment.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1255)
at androidx.fragment.app.FragmentTransition.calculateFragments(FragmentTransition.java:1138)
at androidx.fragment.app.FragmentTransition.startTransitions(FragmentTransition.java:136)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1989)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6718)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我们正在使用androidx.preference:preference-ktx:1.1.1。有人能给我们一个简洁的解释,到底是什么导致了这个问题,以及我们如何解决它吗?

我们已经查看了以下(相关(帖子,但没有任何成功:

  • 片段声明的目标片段不属于此FragmentManager
  • 如何使用新的Android-X API从当前的首选片段打开新的首选片段
  • https://developer.android.com/guide/topics/ui/settings/organize-your-settings

通过导航图创建片段时,它是NavHostFragment的子片段。特别是而不是活动的supportFragmentManager。这就是为什么找不到目标片段的原因——您使用了错误的FragmentManager。

但是,使用"导航"时不应使用app:fragmentonPreferenceStartFragment。相反,PreferencesFragment1应该在Preference上设置一个点击监听器,并让它直接调用navigate()

class PreferencesFragment1 : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences1, rootKey)
findPreference<Preference>("pref2")?.setOnPreferenceClickListener {
// Use whatever ID that is associated with
// PreferenceFragment2 in your navigation graph
findNavController().navigate(R.id.pref2)
}
}
}

首选项1.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<PreferenceCategory
android:title="Category"
app:iconSpaceReserved="false">
<Preference
app:key="pref2"
app:iconSpaceReserved="false"
android:title="Open"/>
</PreferenceCategory>
</PreferenceScreen>

相关内容

  • 没有找到相关文章

最新更新