dialogfragment 中的 setTargetFragment 不起作用 - android
setTargetFragment in dialogfragment not work - android
我在另一个 fragment
中调用 DialogFragment
,如下所示:
@CoursesFragmentScope
class CoursesFragment : DaggerFragment(),
DefaultLifecycleObserver,
CourseTypeListDialogFragment.ApplySelectedCourseTypeFilter {
@OnClick(R.id.btnCourseType)
fun btnCourseTypeClick() {
val fm = activity!!.supportFragmentManager
val courseTypeListDialogFragment =
CourseTypeListDialogFragment()
courseTypeListDialogFragment.isCancelable = false
courseTypeListDialogFragment.setStyle(
DialogFragment.STYLE_NO_TITLE,
0
)
courseTypeListDialogFragment.setTargetFragment(this, 1)
courseTypeListDialogFragment.show(fm, "")
}
}
我的 DialogFragment
如下所示:
class CourseTypeListDialogFragment: DaggerAppCompatDialogFragment(), CourseTypeAdapter.CourseTypeListener {
interface ApplySelectedCourseTypeFilter {
fun applySelectedCourseType()
}
..some code..
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ButterKnife.bind(this, view)
iApplySelectedCourseTypeFilter = targetFragment as ApplySelectedCourseTypeFilter
}
..some code..
}
但是让我在这一行中吼叫 error
:
courseTypeListDialogFragment.setTargetFragment(this, 1)
然后说我:
Fragment CourseTypeListDialogFragment{92f4a96 (cbe1689e-1367-4436-8de6-3797320e7d41) } declared target fragment CoursesFragment{422cc09 (9d16e30b-fd71-44a8-8c45-8a866b62c135) id=0x7f0800d5} that does not belong to this FragmentManager!
我能做什么?
两个类都来自同一个包:
import dagger.android.support.DaggerAppCompatDialogFragment
import dagger.android.support.DaggerFragment
我删除了 courseTypeListDialogFragment.setTargetFragment(this, 1)
并打开了对话框,但我的 interface
在 dialog
中不起作用。
我在 activity
中使用来自 fragments
的内容,如下所示:
class MainActivity : DaggerAppCompatActivity() {
@BindView(R.id.nav_view)
lateinit var navView: BottomNavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super<DaggerAppCompatActivity>.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ButterKnife.bind(this)
val navController = findNavController(R.id.nav_host_fragment)
navView.setupWithNavController(navController)
navView.menu.findItem(R.id.navigation_courses).setIcon(
IconDrawable(this, MaterialCommunityIcons.mdi_account_box)
.colorRes(R.color.colorPrimaryDark)
.actionBarSize())
navView.menu.findItem(R.id.navigation_login).setIcon(
IconDrawable(this, MaterialCommunityIcons.mdi_login)
.colorRes(R.color.colorPrimaryDark)
.actionBarSize())
}
}
更改此行
val fm = activity!!.supportFragmentManager
至
val fm = fragmentManager
阅读以下博客
https://medium.com/better-programming/what-is-target-fragment-da0e7c7f345c
我在另一个 fragment
中调用 DialogFragment
,如下所示:
@CoursesFragmentScope
class CoursesFragment : DaggerFragment(),
DefaultLifecycleObserver,
CourseTypeListDialogFragment.ApplySelectedCourseTypeFilter {
@OnClick(R.id.btnCourseType)
fun btnCourseTypeClick() {
val fm = activity!!.supportFragmentManager
val courseTypeListDialogFragment =
CourseTypeListDialogFragment()
courseTypeListDialogFragment.isCancelable = false
courseTypeListDialogFragment.setStyle(
DialogFragment.STYLE_NO_TITLE,
0
)
courseTypeListDialogFragment.setTargetFragment(this, 1)
courseTypeListDialogFragment.show(fm, "")
}
}
我的 DialogFragment
如下所示:
class CourseTypeListDialogFragment: DaggerAppCompatDialogFragment(), CourseTypeAdapter.CourseTypeListener {
interface ApplySelectedCourseTypeFilter {
fun applySelectedCourseType()
}
..some code..
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
ButterKnife.bind(this, view)
iApplySelectedCourseTypeFilter = targetFragment as ApplySelectedCourseTypeFilter
}
..some code..
}
但是让我在这一行中吼叫 error
:
courseTypeListDialogFragment.setTargetFragment(this, 1)
然后说我:
Fragment CourseTypeListDialogFragment{92f4a96 (cbe1689e-1367-4436-8de6-3797320e7d41) } declared target fragment CoursesFragment{422cc09 (9d16e30b-fd71-44a8-8c45-8a866b62c135) id=0x7f0800d5} that does not belong to this FragmentManager!
我能做什么?
两个类都来自同一个包:
import dagger.android.support.DaggerAppCompatDialogFragment
import dagger.android.support.DaggerFragment
我删除了 courseTypeListDialogFragment.setTargetFragment(this, 1)
并打开了对话框,但我的 interface
在 dialog
中不起作用。
我在 activity
中使用来自 fragments
的内容,如下所示:
class MainActivity : DaggerAppCompatActivity() {
@BindView(R.id.nav_view)
lateinit var navView: BottomNavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super<DaggerAppCompatActivity>.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ButterKnife.bind(this)
val navController = findNavController(R.id.nav_host_fragment)
navView.setupWithNavController(navController)
navView.menu.findItem(R.id.navigation_courses).setIcon(
IconDrawable(this, MaterialCommunityIcons.mdi_account_box)
.colorRes(R.color.colorPrimaryDark)
.actionBarSize())
navView.menu.findItem(R.id.navigation_login).setIcon(
IconDrawable(this, MaterialCommunityIcons.mdi_login)
.colorRes(R.color.colorPrimaryDark)
.actionBarSize())
}
}
更改此行
val fm = activity!!.supportFragmentManager
至
val fm = fragmentManager
阅读以下博客
https://medium.com/better-programming/what-is-target-fragment-da0e7c7f345c