Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MVP : One Activity with Multiple Fragments

I have an Activity that hosts multiple fragments that define each step in a registration flow for a user. The flow is complex and the next step is defined by user actions in the previous steps.

The flow is like below:

MainActivity | Fragment1 --> Fragment2 --> Fragment3 --> Fragment4
                                       \
                                        --> Fragment5 --> Fragment6

Following MVP, I have View & Presenter for each Fragment involved. The concern I have is how to communicate between the Fragment and the Activity in an MVP way. The result of each Fragment needs to be available to the Activity to decide what Fragment needs to be shown next.

One of the approaches I have thought of is to have a listener defined for each fragment which is implemented by the Activity. I somehow find this a little ugly because in the end the Activity needs to implement 6 of these and doesn't look scalable.

What would be a better approach?

like image 485
Anoop Avatar asked Jan 11 '17 10:01

Anoop


Video Answer


2 Answers

In my project I use the fact that each Fragment has access to the parent Activity. My solution is:

enter image description here

like image 129
artkoenig Avatar answered Oct 05 '22 02:10

artkoenig


As you have mentioned that you want to communicate with fragments using MVP.According to me I found 2 solutions.

Solution 1: Callbacks using interfaces, rather creating one interface & bombarding lot of methods that need to be perform on activity/fragment. Try to create more than one interfaces & add methods according to your functionality & implement those according to your use.

Solution 2: Event Buses is the other solution using MVP, where your view observes the bus & picks up event. Following points to take care when dealing with event buses: a. If you are using publishing or subscribers within an Activity of Fragment they should be registered and unregistered with their lifecycle. Otherwise, it's likely you will encounter memory leaks or dangling references that can cause your app to crash.

b. Be wary about publishing events between Fragments. Events cannot be published or received when a Fragment is not running. If you have a Fragment publishing a message to another Fragment that isn't currently executing and then swap one for the other, it's likely the event will not be processed correctly. The EventBus library has a way to replay this event, but the Otto framework does not.

For Event Buses you can refer two libraries: - GreenBot : https://github.com/greenrobot/EventBus - Otto : http://www.vogella.com/tutorials/JavaLibrary-EventBusOtto/article.html

Conclusion If you are dealing with small operations on Activity/Fragments then dealing with interfaces is better option. Event Buses increases your app performance, also dependencies for larger scopes you can go for event buses.

like image 29
Archis Thakkar Avatar answered Oct 05 '22 03:10

Archis Thakkar