我有一个简单的片段导航设置:
<!-- Fragment container in activity that hosts fragments -->
<layout ...>
<data>...</data>
<android.support.v4.widget.DrawerLayout ...>
<android.support.design.widget.CoordinatorLayout ...>
<android.support.v4.widget.NestedScrollView
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout ...>
<android.support.v7.widget.Toolbar .../>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView .../>
</android.support.v4.widget.DrawerLayout>
</layout>
// and this function which replaces fragments
fun fragmentNavigation(fragment: Fragment?, enableBack: Boolean): Boolean {
if (fragment != null) {
val ft = supportFragmentManager.beginTransaction()
.replace(R.id.container, fragment)
if (enableBack) {
// i use null value here because I don't need to
// go back to any specific point just to the last
// fragment added to the back stack
ft.addToBackStack(null)
}
ft.commit()
currentFragment = fragment
return true
}
return false
}
现在的问题是,当我导航到片段并且不将其添加到后台堆栈时,我希望在后退按钮导航管理器上回滚到添加到后台堆栈的最后一个片段,但是我得到并且异常< / p>
// navigation to some other fragment
fragmentNavigation(CustomFragment.newInstance(), false)
// when I click back button I get crash on super.onBackPressed()
override fun onBackPressed() {
return if (binding.drawer.isDrawerOpen(GravityCompat.START)) {
binding.drawer.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed() // this line causes exception
}
}
以下是例外:
Process: com.domain.app, PID: 10938
java.lang.IllegalStateException: ScrollView can host only one direct child
at android.support.v4.widget.NestedScrollView.addView(NestedScrollView.java:406)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1434)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
at android.support.v4.app.BackStackRecord.executePopOps(BackStackRecord.java:855)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2593)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:854)
at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:797)
at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:186)
at com.domain.app.home.HomeActivity.onBackPressed(HomeActivity.kt:113)
at com.domain.app.home.HomeActivity$onToggleBackButton$1.onClick(HomeActivity.kt:185)
at android.support.v7.app.ActionBarDrawerToggle$1.onClick(ActionBarDrawerToggle.java:204)
at android.view.View.performClick(View.java:6891)
at android.view.View$PerformClick.run(View.java:26083)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
答案 0 :(得分:0)
您可以将FrameLyout
作为孩子添加到NestedScrollView
,并将FrameLyout
用作您的容器。因此,片段视图将始终添加到FrameLyout
,而NestedScrollView
只会有一个直接子项。
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" >
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.NestedScrollView>