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
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.
The main distinction between MVP and MVC is that the Presenter is more involved in UI logic than a Controller in MVC.
Let's consider a simple example where we display a user's name.
This class represents the data and can interact with databases, network operations, etc.
data class User(val name: String)
Define an interface that the Activity/Fragment will implement.
interface UserView { fun showUserName(name: String) }
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) } }
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 } }
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.
Implementing MVP in Android example code:
// 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()); } }
Handling user input and interactions in MVP Android:
// 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); } }
Data binding in MVP architecture for Android:
// In UserPresenter public void updateView() { view.displayUser(model.getUser()); }