Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

Android - getTargetFragment and setTargetFragment - What are they used for

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use case = 2 fragments hosted by the same activity.

Where startActivityForResult() establishes a relationship between 2 activities, setTargetFragment() defines the caller/called relationship between 2 fragments.

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...