1
votes

I am experience Android Developer of almost 2.5 years, who was simple writing all codes in Activity class.

Recently I came across many good things about development that will make developer life awesome.

  1. Retrofit
  2. RxJava/RxAndroid
  3. Dagger
  4. MVP pattern
  5. Testing code (Unit Test)

What I learned (at least to start with project)

  1. Retrofit

  2. RxJava/RxAndroid

Whats my confusion about MVP

There are people who follow the code style

  • View (Interface)
  • Presenter (Interface)
  • PresenterImplementation (Class)
  • Interactor (Interface)
  • InteractorImplementation (Class)

But , I follow

  • View (Interface)
  • Presenter (Class) --- implements --> [ PresenterForView (Interface) & PresenterForInteractor (Interface) ]
  • PresenterForView (Interface) -- Its works for View (eg . login button clicked in View i.e, Activity) . Takes login click from View and gives to Interactor
  • PresenterForInteractor (Interface) -- Its works for Interactor (eg . gets response from Interactor like success , failure to show in View i.e, Activity) .
  • Interactor (Class) : does validation logics , calls webservice (Rxjava & Retrofit used) , returns the NoNetwork,TimeOut,Failure, Success response to PresenterForInteractor

Many people say that Logics (if..else) should only be written in Interactor . Your View(Activity) , Presenter should not contain (If else statement) app logic.

There many tutorials found on Medium in which people do anything . They write half app logic in Presenter , rest in Interactor

I tried to follow the First Option of writing every app login in Interactor

But there are scenario where I get confused where to write app login

  • onActivityResult
  • onRequestPermission
  • using data in 2nd Activity got from 1st Actiity via Intent.putExtras (which I always use in OnCreate() of 2nd Activity)
1
Could you post a link to those tutorials? All I can say for now is that MVP works totally fine without Interactors. So you don't have to worry if you don't use them. - daemmie

1 Answers

0
votes

On two use cases,

  • onActivityResult
  • onRequestPermission

I distinguish logic in a way that, if that logic do anything on UI - UI logic(e.g: update Text, showing dialog, animation, etc.) is belong to the Activity/Fragment. Because you are just on that Activity/Fragment, then you may just call it right away.

If you need to operate any business logic, such as: update the database, network request/response, etc. You may need to call it via Presenter. E.g:

presenter.getDataFromTheWeb()

  • using data in 2nd Activity got from 1st Actiity via Intent.putExtras (which I always use in OnCreate() of 2nd Activity)

To retrieve data in Activity/Fragment uses getArgument(), which attached with its Activity/Fragment. So that you retrieve data in Activity class makes sense.

In overall, MVP is a great way to de-couple view logic/application logic and business logic, also helps you to test your app more easily. However using right pattern depends on particular problem you solve. If your business logic is also complex, you need Interactor pattern.