Professional Documents
Culture Documents
http://www.technotalkative.com/part-4-playing-with-navigationview/navigationview-android-design-
support-library/
First, we will create an abstract layer, and after this we will move to implementation.
Let’s start with creation of base classes which will help us to follow MVP pattern.
❤ i
https://github.com/KucherenkoIhor/DelegationActivityTemplate/blob/master/app/src/main/java/com/ki/dat
/delegationactivitytemplate/base/BaseActivity.java
All Activities in our project will inherit from this class. You can create a similar class for
Fragments. I use ButterKnife for view binding.
And this is a base view interface. All Activities or Fragments will implement it.
You can check out all these classes in base package of the template. It demonstrates a
basic Model‑View‑Presenter (MVP) architecture and provides a foundation on which
the sample is built. I also recommend you visit Android Architecture Blueprints for
more details.
https://proandroiddev.com/clean-your-activity-using-delegation-pattern-fcaafd82336d 4/12
20/06/2022, 08:46 Clean Your Activity Using Delegation Pattern | by Ihor Kucherenko | ProAndroidDev
http://macoscope.com/blog/model-view-presenter-architecture-in-android-applications/
To reduce the amount of code in Activity, we will use the Delegation pattern. The point
is that an instance handles a request by delegating to a second object (the delegate).
Also, we have to keep in mind the fact that we deal with Android and adapt our
Delegate class to Activity Lifecycle.
http://abhiandroid.com/programming/activity-life-cycle
https://proandroiddev.com/clean-your-activity-using-delegation-pattern-fcaafd82336d 5/12
20/06/2022, 08:46 Clean Your Activity Using Delegation Pattern | by Ihor Kucherenko | ProAndroidDev
First, let’s create base classes which will handle main challenges.
Open in app Get started
This is the base class for delegates which contains Presenter and View instances, so we
can deal with different functionality. I use generics for universalization and ButterKnife
for access to views.
This abstract class extends BaseActivity and handles challenges with lifecycle.
Inheritors of this Activity will delegate tasks to heirs of BaseActivityDelegate. Let’s take
a look at the result:
https://github.com/KucherenkoIhor/DelegationActivityTemplate/blob/master/app/src/main/java/com/ki/dat
/delegationactivitytemplate/delegation/NavigationDrawerDelegate.java
9
10 public static Intent newIntent(Context context) { Open in app Get started
11 return new Intent(context, NavigationDrawerActivity.class);
12 }
13
14 @Override
15 protected NavigationDrawerDelegate instantiateDelegateInstance() {
16 return new NavigationDrawerDelegate();
17 }
18
19 @NonNull
20 @Override
21 protected NavigationDrawerPresenter getPresenterInstance() {
22 return new NavigationDrawerPresenter();
23 }
24
25 @Override
26 protected void onCreate(Bundle savedInstanceState) {
27 setContentView(R.layout.activity_navigation_drawer);
28 setSupportActionBar(mToolbar);
29 super.onCreate(savedInstanceState);
30 }
31
32 @Override
33 public NavigationDrawerActivity getActivity() {
34 return this;
35 }
36
37 @OnClick(R.id.fab)
38 public void onFabClick(View view) {
39 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
40 .setAction("Action", null).show();
41 }
42
43 @Override
44 public void onBackPressed() {
45 if (!mDelegate.closeDrawer()) {
46 super.onBackPressed();
47 }
48 }
49
50 @Override
51 public void onSomethingDone() {
52 Snackbar.make(getContentView(), "Done", Snackbar.LENGTH_LONG)
53 .setAction("Action", null).show();
54 }
55
56 @Override
https://proandroiddev.com/clean-your-activity-using-delegation-pattern-fcaafd82336d 9/12