当 fragment 成为 backstack 的顶部时与它通信?
Communicating with fragment when it becomes the top of the backstack?
我有两个片段:FragmentA
和 FragmentB
。我从我的 ActivityMain
开始 FragmentA
,然后我开始 FragmentB
而不从后台弹出 FragmentA
(我想在 [=11] 的背景中显示 FragmentA
=]).最终我完成了 FragmentB
并将其从后台弹出,但我需要通知片段 A FragmentB
不再使用。
如何与 FragmentA
沟通 FragmentB
已关闭并且 FragmentA
现在位于后台堆栈的顶部?
我试过在许多 Fragment 的 类 中放置断点,例如 OnResume()、OnAttach、OnStart(),但没有任何结果。
你应该使用 setTargetFragment 方法。
Optional target for this fragment. This may be used, for example, if
this fragment is being started by another, and when done wants to give
a result back to the first. The target set here is retained across
instances via.
回到你的案例,当从 FragmentA
开始 FragmentB
时。
class FragmentA : Fragment() {
companion object {
val REQUEST_CODE_FRAGMENT_A = 1
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
val view = inflater?.inflate(R.layout.fragment_a, container, false)!!
val fragmentB = FragmentB().apply {
setTargetFragment(this@FragmentA, REQUEST_CODE_FRAGMENT_A)
}
fragmentManager.beginTransaction()
.add(R.id.container, fragmentB, "fragB")
.addToBackStack(null)
.commit()
return view
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_FRAGMENT_A) {
if (resultCode == Activity.RESULT_OK) {
val message = data?.getStringExtra("message")
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
}
}
}
}
当FragmentB
从后台弹出时,onDestroy
方法将被调用。
class FragmentB : Fragment() {
override fun onDestroy() {
// Notify for FragmentA (as target fragment) that FragmentB destroyed.
val intent = Intent().apply { putExtra("message", "I'm out.") }
targetFragment.onActivityResult(targetRequestCode, Activity.RESULT_OK, intent)
super.onDestroy()
}
}
我有两个片段:FragmentA
和 FragmentB
。我从我的 ActivityMain
开始 FragmentA
,然后我开始 FragmentB
而不从后台弹出 FragmentA
(我想在 [=11] 的背景中显示 FragmentA
=]).最终我完成了 FragmentB
并将其从后台弹出,但我需要通知片段 A FragmentB
不再使用。
如何与 FragmentA
沟通 FragmentB
已关闭并且 FragmentA
现在位于后台堆栈的顶部?
我试过在许多 Fragment 的 类 中放置断点,例如 OnResume()、OnAttach、OnStart(),但没有任何结果。
你应该使用 setTargetFragment 方法。
Optional target for this fragment. This may be used, for example, if this fragment is being started by another, and when done wants to give a result back to the first. The target set here is retained across instances via.
回到你的案例,当从 FragmentA
开始 FragmentB
时。
class FragmentA : Fragment() {
companion object {
val REQUEST_CODE_FRAGMENT_A = 1
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
val view = inflater?.inflate(R.layout.fragment_a, container, false)!!
val fragmentB = FragmentB().apply {
setTargetFragment(this@FragmentA, REQUEST_CODE_FRAGMENT_A)
}
fragmentManager.beginTransaction()
.add(R.id.container, fragmentB, "fragB")
.addToBackStack(null)
.commit()
return view
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CODE_FRAGMENT_A) {
if (resultCode == Activity.RESULT_OK) {
val message = data?.getStringExtra("message")
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
}
}
}
}
当FragmentB
从后台弹出时,onDestroy
方法将被调用。
class FragmentB : Fragment() {
override fun onDestroy() {
// Notify for FragmentA (as target fragment) that FragmentB destroyed.
val intent = Intent().apply { putExtra("message", "I'm out.") }
targetFragment.onActivityResult(targetRequestCode, Activity.RESULT_OK, intent)
super.onDestroy()
}
}