Reputation: 384
I am looking into MVP architecture Implementation in android. I found too many ways(mention end of the question) to implement it in the android studio, but Still, I am confused.
Can someone help me to find the right answer of below questions.
Upvotes: 0
Views: 848
Reputation: 5530
You can use either Activity
or Fragment
for View layer. This is because showing UI elements in android needs Context
.
For the Presenter layer, you must make sure not to pass the Context
to the Presenter via constructor or setter. If you needed Context
in your Presenter for tasks other than showing the UI, such as writing to SharedPreferences
, you can get it from your View (which is either Activity
or Fragment
). In this way, if the View gets destroyed or becomes null
, there would no standalone null
Context
in the Presenter to cause leak issues.
If you want to know more about the MVP structure, I have written a very handy MVP library for android and explained its use in a sample app here.
Upvotes: 1
Reputation: 7
MVP android sample example application MVP Android Example used to explain how to use this pattern in our Android apps.
Upvotes: 0
Reputation: 82938
What would be directory structure of Application in MVP?
Activity should be a Presenter or View?
If you are looking for a good example of MVP implementation, there is one GitHub Repo for MVP developed by Android itself. Which you should look into.
Where
Upvotes: 3
Reputation: 2004
Though there are lot of implementations for an MVP architecture, all of them share a basic idea (or they should at least), which is separating business logic from your views (activities, fragments, dialogs). Why is that? Well, for two reasons mainly:
About your questions:
What would be directory structure of Application in MVP?
There is no rule about that except that your MVP components should be identified. Here you have an article where I started with a package structure but then I found other more convenient.
Activity should be a Presenter or View?
Your activity (or fragment or whatever components in charge of showing view components) should be the one that implements your view.
My advice is that you should check multiples examples and see their advantages and disadvantages of each one, and try to define your own architecture from those which you will feel more comfortable with.
Upvotes: 1