Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

MVP (Model View Presenter) Architecture Pattern in Android with Example

The MVP (Model-View-Presenter) pattern is an evolution of the MVC pattern tailored more for platforms like Android where the View and Controller often get muddled together, particularly in Activities and Fragments.

Components:

  1. Model: Represents the data source and business logic.
  2. View: Displays the data and forwards user commands to the Presenter.
  3. Presenter: Retrieves data from the Model, applies the UI logic, and manages the data that the View displays.

The main distinction between MVP and MVC is that the Presenter is more involved in UI logic than a Controller in MVC.

MVP in Android:

  • Model: Represents data storage, retrieval, and business logic.
  • View: Typically represented by an Activity, Fragment, or a custom Android View. This component only displays the data it is given by the Presenter and delegates user interactions to the Presenter.
  • Presenter: Manages the data flow from the Model to the View, and vice versa. It holds the UI logic without having any Android-specific code.

Example:

Let's consider a simple example where we display a user's name.

Model:

This class represents the data and can interact with databases, network operations, etc.

data class User(val name: String)

View:

Define an interface that the Activity/Fragment will implement.

interface UserView {
    fun showUserName(name: String)
}

Presenter:

The Presenter contains the logic to fetch and display the user.

class UserPresenter(private val view: UserView) {
    
    private val user = User("John Doe") // This could be fetched from a database or API
    
    fun displayUser() {
        view.showUserName(user.name)
    }
}

Implementation:

Now, let's tie it all together with an Activity:

class UserActivity : AppCompatActivity(), UserView {

    private lateinit var presenter: UserPresenter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user)

        presenter = UserPresenter(this)
        presenter.displayUser()
    }

    override fun showUserName(name: String) {
        // Display the user's name in a TextView
        userNameTextView.text = name
    }
}

Advantages of MVP:

  1. Separation of Concerns: Clear separation between business logic and UI.
  2. Testability: By decoupling the business logic from Android's components, testing becomes much more straightforward. You can test the Presenter without Android instrumentation by mocking the View.
  3. Reusability: Business logic can be reused across different parts of the app or even different platforms.

Disadvantages:

  1. Overhead: For simpler applications or screens, MVP can feel like overkill, adding complexity.
  2. Learning Curve: There's a learning curve, especially for developers new to the concept.

MVP has been popular in Android development, but with the introduction of Android's data binding and architectural components, patterns like MVVM (Model-View-ViewModel) have gained more traction lately.

  1. Implementing MVP in Android example code:

    • Create separate classes for Model, View, and Presenter.
    // Model
    public class UserModel {
        // Model logic
    }
    
    // View
    public interface UserView {
        void displayUser(String user);
    }
    
    // Presenter
    public class UserPresenter {
        private UserModel model;
        private UserView view;
    
        public UserPresenter(UserModel model, UserView view) {
            this.model = model;
            this.view = view;
        }
    
        public void updateView() {
            view.displayUser(model.getUser());
        }
    }
    
  2. Handling user input and interactions in MVP Android:

    • The Presenter handles user input and updates the Model accordingly.
    // Example in Android Activity
    public class UserActivity extends AppCompatActivity implements UserView {
        private UserPresenter presenter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            UserModel model = new UserModel();
            presenter = new UserPresenter(model, this);
    
            // Handle user interactions
            button.setOnClickListener(v -> presenter.updateView());
        }
    
        @Override
        public void displayUser(String user) {
            // Update UI with user data
            textView.setText(user);
        }
    }
    
  3. Data binding in MVP architecture for Android:

    • MVP doesn't inherently focus on data binding, but you can manually update the view when the model changes.
    // In UserPresenter
    public void updateView() {
        view.displayUser(model.getUser());
    }