Use of MVP in Android
Posted By : Prince Bhardwaj | 25-Apr-2018
What exactly is the MVP?
The MVP is that pattern that permits isolate introduction layer from the logic, with the goal that how to interface functions get isolated from the how we exhibit it on the screen. At first, MVP example can accomplish a similar logic that may be totally unique and exchangeable perspectives. Like most of the architectural patterns, MVP is open for a lot of various experiments.
1.It separates the code for views, which is used for showing for the user like list, buttons, labels etc. from the business logic that is triggered on user interaction with a view which we call a presenter. Data that is shown in the view is provided by another separate module which is called as a model.
2. Communication between the view and modal takes place through presenter i.e. view and modal can't have each other's reference.
3. By using this design we can make changes in a component without changing the code of other components.
4. The code is maximized that can be tested with automation.
Andriod activity and MVP
Andriod activity gives the functionality of view like we do things setContentView() in OnCreate(). Activities maintain andriod life cycle events such as onCreate() onResume() etc which will be business logic i.e presenter.
Another way of showing how they are entangled is:-
1. registers the listeners for the user interaction with the view.
2. Perform the required actions on user interactions with business logic.
Implementation of MVP
The presenter's interaction with model gets handled by separate manager class. It follows the contract of VideoListManager
android {
public interface VideoListManager {
interface VideoListManagerListener {
void onVideoListUpdate(VideoListInfo videoListInfo);
}
void getVideosWithNewSorting(int sortType);
void registerListener(VideoListManagerListener videoListManagerListener);
void unRegisterListener();
}
View
Videos are displayed by use of ListView or ExpandableListView. The use of ViewMvp interface returns root view to the presenter that is further used for communications among presenter and view for user interactions.
We use ListView so we need to implement by using adapters.
android {
@Override
public void bindVideoList(VideoListInfo videoListInfo)
{
mVideoListAdapter.bindVideoList(videoListInfo);
mVideoListAdapter.notifyDataSetChanged();
}
@Override
public View getRootView() {
return mFragemntVideoListView;
}
Presenter
It is the important part of the application, that initializes view and model and it also set up the communication framework for handling users interactions and model data changes
android {
VideoListingViewImpl mVideoListingViewImpl;
VideoListManagerImpl mVideoListManagerImpl;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mVideoListingViewImpl = new VideoListingViewImpl(this, null); //initializing the view
setContentView(mVideoListingViewImpl.getRootView()); //setting the setContentView from the rootView returned
mViewPager = mVideoListingViewImpl.getViewPager();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
mSortingType = settings.getInt(SORT_TYPE_PREFERENCE_KEY, 3);
mVideoListManagerImpl = new VideoListManagerImpl(this, mSortingType);// initialising the model
mVideoListManagerImpl.registerListener(this); // registering the listerner to get video list update events
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Prince Bhardwaj
Prince Bhardwaj is having Talent as Application Developer, he is an enthusiastic team player in Oodles Technologies.