“This is the 16th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

preface

When we use an Activity, we can start a new Activity with startActivityForResult, and then we can use setResult in the new Activity to send back some result information to the previous Activity. The previous Activity gets this information in onActivityResult.

What about when we use fragments as pages? We know we can use setArguments to send information to the fragments that follow, but how do we post results back?

setTargetFragment

Android provides a setTargetFragment method

public void setTargetFragment(Fragment fragment, int requestCode)
Copy the code

GetTargetFragment () and getTargetRequestCode() can be used to get the corresponding Fragment and Code.

When we need to send back information, we can call onActivityResult as follows:

getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intentData);
Copy the code

So the previous Fragment can handle the postback information in its onActivityResult.

Create a setTargetFragment. Create a setTargetFragment. Create a 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 {@link FragmentManager#putFragment * FragmentManager.putFragment()}. * * @param fragment The fragment that is the target of this one. * @param requestCode Optional request code, for convenience if you * are going to call back with {@link #onActivityResult(int, int, Intent)}. */ public void setTargetFragment(Fragment fragment, int requestCode) { // Don't allow a caller to set a target fragment in another FragmentManager, // but there's a snag: people do set target fragments before fragments get added. // We'll have the FragmentManager check that for validity when we move // the fragments to a valid state. final FragmentManager mine = getFragmentManager(); final FragmentManager theirs = fragment ! = null ? fragment.getFragmentManager() : null; if (mine ! = null && theirs ! = null && mine ! = theirs) { throw new IllegalArgumentException("Fragment " + fragment + " must share the same FragmentManager to be set as a target fragment"); } // Don't let someone create a cycle. for (Fragment check = fragment; check ! = null; check = check.getTargetFragment()) { if (check == this) { throw new IllegalArgumentException("Setting " + fragment + " as the target of " + this + " would create a target cycle"); } } mTarget = fragment; mTargetRequestCode = requestCode; } /** * Return the target fragment set by {@link #setTargetFragment}. */ final public Fragment getTargetFragment() { return mTarget; } /** * Return the target request code set by {@link #setTargetFragment}. */ final public int getTargetRequestCode() { return mTargetRequestCode; }Copy the code

The setTargetFragment performs two rounds of validation. First, ensure that the two fragments are the same FragmentManager. Second, ensure that the TargetFragment is not the same as the current Fragment. So setTargetFragment is more secure, and Android already provides it, so we don’t need to implement it ourselves.