简体   繁体   中英

MVP approach - Android

I am trying to implement MVP architecture in a sample android project .

Could you recommend how I can break this code down for a better approach on MVP.

I can create an Interface to have setContactInfo() method but could not think of any other approach.

This is detailView of the List item from the list view. I am getting the data in a Bundle from another activity.

Thank you for your advice.

  package com.salesi.coding;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ContactDetails extends AppCompatActivity {

    @Bind(R.id.tvTitle) protected TextView mTitle;
    @Bind(R.id.tvFirstName) protected TextView mFirstName;
    @Bind(R.id.tvLastName) protected TextView mLastName;
    @Bind(R.id.tvJobTitle) protected TextView mJobTitle;
    @Bind(R.id.tvPhoneNUmber) protected TextView mPhoneNUmber;
    @Bind(R.id.tvEmail) protected TextView mEmail;
    @Bind(R.id.tvHobbies) protected TextView mHobbies;
    @Bind(R.id.tvAddressLine1) protected TextView mAddressLine1;

    Intent contactIntent;
    Bundle contactBundle;
    String title, firstName, lastName,jobTitle, phoneNumber, email, address, hobbies;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_details);

        ButterKnife.bind(this);

        //Get the Bundle data.
        contactIntent = getIntent();
        contactBundle = contactIntent.getExtras();

        hobbies = contactBundle.getString("hobbies");
        title = contactBundle.getString("title");
        firstName = contactBundle.getString("firstName");
        lastName = contactBundle.getString("lastName");
        jobTitle = contactBundle.getString("jobTitle");
        phoneNumber = contactBundle.getString("phoneNumber");
        email = contactBundle.getString("email");
        address = contactBundle.getString("address");
        setContactInfo();

    }

    private void setContactInfo(){
        mTitle.setText(title);
        mFirstName.setText(firstName);
        mLastName.setText(lastName);
        mJobTitle.setText(jobTitle);
        mPhoneNUmber.setText(phoneNumber);
        mEmail.setText(email);
        mHobbies.setText(hobbies.toString());
        mAddressLine1.setText(address.toString());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.exitApp:
                finish();
                break;

            default:
        }
        return true;
    }
}

So, before MVP became really popular, Android projects were written in a manner in which the activity was too heavy(doing too much work). Activities would be responsible for rendering UI, handling touch events, making API calls, having callbacks for said API calls etc.

One of the reasons why MVP became popular was because it introduced separation of concerns into Android code. Basically, in MVP the activity/fragment(the View) is incharge of only the view related aspects, while things like making API calls and handling the callbacks were removed and moved to the presenter.

The code you provided above is actually fine as it is and doesn't require a presenter.

Here's a scenario of when you would use MVP:

The contact details are coming from your backend server. ContactDetailsActivity would inform ContactDetailsPresenter that the details are required. ContactDetailsPresenter would make a GET request to the backend server. Once ContactDetailsPresenter gets a successful response from the server, it would inform the ContactDetailsActivity that the details have been fetched and would provide the necessary details to ContactDetailsActivity. ContactDetailsActivity would update the UI with the details.

For an MVP (model-view-presenter) architectural pattern you should have : A model , A VIEW , A presenter

first of all you have the model part and you have this correctly done which is the xml files ,

second a view and it's the place to bind the attributes to the model parts which you are doing in the code above

Third the pattern which is the place you should retrieve info , so what you can do is do a separate class in which you put the methods above import it in your class and call the methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM