Last active
December 5, 2021 19:40
-
-
Save dakatso/36c1147e22d07bce60ba398a0bf37c44 to your computer and use it in GitHub Desktop.
FragmentResultListener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Fagment.fragmentListeners(block: FragmentListenerBuilder.() -> Unit) { | |
val builder = FragmentListenerBuilder().apply(block) | |
builder.keys.forEach { | |
val manager = if (it.value) childFragmentManager else parentFragmentManager | |
manager.setFragmentResultListener(it.key, this, builder.listener) | |
} | |
} | |
class FragmentListenerBuilder { | |
var listener: (String, Bundle) -> Unit = { _, _ -> } | |
val keys = mutableMapOf<String, Boolean>() | |
fun child(key: String) { | |
keys[key] = true | |
} | |
fun parent(key: String) { | |
keys[key] = false | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fragmentListeners { | |
child(KEY_FOR_CHILD_FRAGMENT_MANAGER) | |
parent(KEY_FOR_PARENT_FRAGMENT_MANAGER) | |
listener = { requestkey, bundle -> | |
when(requestkey){ | |
KEY_FOR_CHILD_FRAGMENT_MANAGER -> //do something | |
KEY_FOR_PARENT_FRAGMENT_MANAGER -> //do something | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment