Sobes.tech
Junior — Middle

Could you explain what is meant by the term Target Fragment in the context of Android development?

sobes.tech AI

Answer from AI

In the context of Android development, the term Target Fragment usually refers to a fragment that is the recipient of a result or interaction from another fragment.

When one fragment launches another (for example, to get data or make a selection), it can set itself as the "target fragment" of the called fragment. This allows the called fragment to send a result back to the caller.

Example of usage:

// In the calling fragment
val fragment = SomeDialogFragment()
fragment.setTargetFragment(this, REQUEST_CODE)
fragment.show(parentFragmentManager, "dialog")

// In SomeDialogFragment upon completion
val target = targetFragment
if (target != null) {
    val intent = Intent()
    intent.putExtra("result", data)
    target.onActivityResult(targetRequestCode, Activity.RESULT_OK, intent)
}

Thus, Target Fragment is a fragment that expects to receive a result from another fragment, enabling communication between them without the need to use an Activity as an intermediary.

Could you explain what is meant by the term Target… - sobes.tech