Android : best case for communicating adapter with Activity/Fragment

1.4k views Asked by At

In MVP pattern, action from user must deliver to presenter

so in listView when user click on item I want send this action to presenter

what is the best case for communicating adapter with Activity/Fragment?

and I appreciate it if you explain pros and cond of each one.

  1. EventBus
  2. CallBack
3

There are 3 answers

0
Rissmon Suresh On BEST ANSWER

Depending on your scenario u can adopt given below methods

Try using an interface

https://stackoverflow.com/a/16443645/4247543

if the above method is not helping then try using EventBus

please follow below link to know more of Event Bus

https://github.com/greenrobot/EventBus

http://gunhansancar.com/ease-communication-between-activities-fragments-services/

BroadcastReceiver as mentioned could also help for enabling communication.

https://stackoverflow.com/a/10084754/4247543

You can use observers

https://stackoverflow.com/a/30964385/4247543

Hopes it helps.

0
ʌɐɥqıɐʌ On

You can always use callbacks for listening clicks. You should implement is using interfaces.

1
Zain On

The communication between View & Presenter should be through an interface.

Both Presenter and Activity (view) have their own interface.

  1. So a List item click item is forward to a Presenter.
  2. The Presenter decides what to do. i.e. open a DetailsActicity
  3. Call's the Activity to start new Intent.

Here's a good example of MVP interfaces

A good/brief explanation of MVP

Edit

  • Communication between Adapter & Activity/Fragment.

First point - in my personal view Adapter & Activity/Fragmentare all subsections of View in the MVP architecture.

The Activity will hold reference to the Adapter

The Activity (via the Presenter) can mainpualte the data in the adapter by the usual means i.e. changing the underlying data object and calling notifyDataSetChanged.

As for the Adapter sending requests back to the Activity i.e. View.OnClickListener this can be done via callbacks that are sent when the Adapter is first initialised (i.e. created using the new keyword)