Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 378
j2emanue Avatar asked Sep 01 '13 17:09

j2emanue


People also ask

What is setTargetFragment?

setTargetFragment(target) lets the "called" fragment know where to send the result. onActivityResult() is called manually in this case.

What is a target fragment Android?

A fragment is a piece of an application's user interface or behavior that can be placed in an activity. Interaction with fragments is done through FragmentManager, which can be obtained via Activity. getFragmentManager() and Fragment.

What is retained fragment in Android?

A Fragment represents a reusable portion of your app's User Interface. Retained Fragment consists of the configuration change that causes the underlying Activity to be destroyed. The term "retained" refers to the fragment that will not be destroyed on configuration changes.

What does the onCreateView () method return if a fragment doesn't have any UI?

These files contain only the onCreateView() method to inflate the UI of the fragment and returns the root of the fragment layout. If the fragment does not have any UI, it will return null.


2 Answers

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) 
like image 139
kandinski Avatar answered Nov 07 '22 14:11

kandinski


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).

like image 20
j2emanue Avatar answered Nov 07 '22 12:11

j2emanue