DEV Community

Communication between Fragment and Activity

Khoa Pham on December 21, 2017

Original post https://github.com/onmyway133/blog/issues/108 There's always need for communication, right 😉 Suppose we have OnboardingActivity that...
Collapse
 
sindrenm profile image
Sindre Moen

If I had a direct reference to the OnboardingFragment somewhere in my activity, I suppose I would prefer the lambda. However, through onAttachFragment you'd need to check the type, which I feel looks kind of ugly.

Also, with the interface, I'd instead have a setter on my fragment instead of checking the type of context. That way, the listener doesn't necessarily need to be an Activity or Context, but anything that implements the interface.

Collapse
 
nottylerjames profile image
Tyler Healey

Have you found a clean way of implementing a Coordinator/Flow Controller pattern on Android? Seems like you'd have one Activity as the flow controller and multiple fragments that communicate back to the Activity. Since the Lambda doesn't work I suppose the delegate pattern would be best.

As a side note I should really get into Kotlin, it's so Swift-like that it took me a few seconds to realize this was an Android article.