问题描述
我尝试过搜索,但还是有点迷茫.我通常通过接口或 BroadcastReceiver
通过 Activity
进行片段到片段的通信.
I tried searching but i'm still a little lost. I usually do fragment to fragment communication through an Activity
via interfaces or a BroadcastReceiver
.
无论如何,我的问题是getTargetFragment
有什么用?有人可以提供一个用例或只是一个简单的例子,以便我可以理解它的用法吗?
Anyway, my question is what is the use of getTargetFragment
? Can someone provide a use case or just a quick example so i can comprehend its usage?
推荐答案
用例 = 由同一活动托管的 2 个片段.
Use case = 2 fragments hosted by the same activity.
其中 startActivityForResult()
建立了 2 个 Activity 之间的关系,setTargetFragment()
定义了 2 个片段之间的调用者/被调用者关系.
Where startActivityForResult()
establishes a relationship between 2 activities, setTargetFragment()
defines the caller/called relationship between 2 fragments.
setTargetFragment(target) 让被调用的"片段知道将结果发送到哪里.onActivityResult()
在这种情况下被手动调用.
setTargetFragment(target) lets the "called" fragment know where to send the result. onActivityResult()
is called manually in this case.
public class Caller extends Fragment
Fragment called = Called.newInstance()
called.setTargetFragment(this)
public class Called extends DialogFragment
intent = amazingData
getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent)
这篇关于Android - getTargetFragment 和 setTargetFragment - 它们的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!