Android - getTargetFragment and setTargetFragment - What are they used for
Asked Answered
S

3

45

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?

Substance answered 1/9, 2013 at 17:41 Comment(3)
https://mcmap.net/q/135482/-callback-to-a-fragment-from-a-dialogfragment here is another good example... but interfaces are also good.Accouchement
Thanks ...it looks like just a place holder for a fragment. so if i have a fragment and i setTarget then i can use getTarget to retrieve the fragment. Its similar to setTag on views.Substance
Instead of BroadcastReceiver, consider using LocalBroadcastManager (here: developer.android.com/reference/android/support/v4/content/… ) , or create your own mechanism of events handling.Mccormack
E
54

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)
Erkan answered 1/9, 2015 at 4:57 Comment(0)
S
15

i finally found out how to use setTarget in a fragment and wanted to share. its quite useful when you want to communicate from fragment to fragment.

here is an example: let's say you wanted to show a dialog and when it closes you want to do some action.

so in your fragment1 that will use the dialog you could do this:

myDialogFragment.setTargetFragment(fragment1, myDialogFragment.REQ_CODE);

and in your fragment that called the dialog you would need to override onActivityResult like this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == CoDDialogFragment.REQ_CODE)
        exit(); //or whatever you want to do here
}

and in the myDialogFragment you could do this:

TellTargetYouGotResults(REQ_CODE);

//...
private void TellTargetYouGotResults(int code) {
    Fragment targetFragment = getTargetFragment(); // fragment1 in our case
    if (targetFragment != null) {
        targetFragment.onActivityResult(getTargetRequestCode(), code, null);
    }
}

where REQ_CODE can be any int of course . Very useful for fragment to fragment communication. but i still prefer event bus as sometimes after sending data to a target its view might have already been destroyed (incase its a fragment) and then if you try to update the view in onActivityResult you'll get a crash. so i'd say its useful to just pass data along but not update the UI unless you've done a 'add' fragment transaction and not a replace (which destroys the view but keeps state).

Substance answered 10/8, 2017 at 11:45 Comment(2)
I do not understand what you are trying to say with "as sometimes after sending data to a target its view might have already been destroyed (incase its a fragment)". I rarely experience crashes in my application, when I call getTargetFragment().onActivityResult() because the return value of getTargetFragment() is null but I can not reproduce this problem. So I want you to closer describe, what you really mean by this part of the sentence.Wakeup
What a fragment transaction replace is done with add to backstack....the view part is destroyed. Just the state of the fragment exists. View is inflated again. So if communicating between these two fragments be careful not to undate it's view part of you know it does not exist.Substance
D
4

From what I was able to take away from reading the docs was that these methods are another way of accessing data from another Fragment. Here is a sample project that I wrote that demonstrates a single use case for using these methods. I'm sure there are more use cases though...

Dissonant answered 1/9, 2013 at 23:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.