You are on page 1of 8

20/06/2022, 08:46 Clean Your Activity Using Delegation Pattern | by Ihor Kucherenko | ProAndroidDev

Open in app Get started

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.

1 public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity


2 implements BaseView {
3
4 private Unbinder mUnBinder;
5
6 private ProgressDialog mProgressDialog = null;
7
8 protected @NonNull abstract P getPresenterInstance();
9
10 protected P mPresenter;
11
12 @Override
13 protected void onCreate(@Nullable Bundle savedInstanceState) {
14 super.onCreate(savedInstanceState);
15 mPresenter = getPresenterInstance();
16 mPresenter.attachView(this);
17 }
18
https://proandroiddev.com/clean-your-activity-using-delegation-pattern-fcaafd82336d 2/12
20/06/2022, 08:46 Clean Your Activity Using Delegation Pattern | by Ihor Kucherenko | ProAndroidDev
18
19 @Override
Open in app Get started
20 public void setContentView(@LayoutRes int layoutResID) {
21 super.setContentView(layoutResID);
22 mUnBinder = ButterKnife.bind(this);
23 }
24
25 @Override
26 protected void onDestroy() {
27 mPresenter.detachView();
28 mUnBinder.unbind();
29 super.onDestroy();
30 }
31
32 @Override
33 public View getContentView() {
34 return getWindow().getDecorView();
35 }
36 }

❤ 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.

1 public interface BasePresenter<V extends BaseView> {


2
3 void attachView(V view);
4
5 void detachView();
6
7 }

BasePresenter.java hosted with ❤ by GitHub view raw


https://github.com/KucherenkoIhor/DelegationActivityTemplate/blob/master/app/src/main/java/com/ki/dat
/delegationactivitytemplate/base/BasePresenter.java

1 public class BasePresenterImpl<V extends BaseView> implements BasePresenter<V> {


2
3 protected V mView;
4
5 @Override
6 public void attachView(V view) {
7 mView = view;
8 }
9
https://proandroiddev.com/clean-your-activity-using-delegation-pattern-fcaafd82336d 3/12
20/06/2022, 08:46 Clean Your Activity Using Delegation Pattern | by Ihor Kucherenko | ProAndroidDev
9
10 @Override Open in app Get started
11 public void detachView() {
12 mView = null;
13 }
14
15 public V getView() {
16 return mView;
17 }
18
19 }

BasePresenterImpl.java hosted with ❤ by GitHub view raw


https://github.com/KucherenkoIhor/DelegationActivityTemplate/blob/master/app/src/main/java/com/ki/dat
/delegationactivitytemplate/base/BasePresenterImpl.java

This is a base presenter interface and simple implementation which assumes


interaction with the view instance.

1 public interface BaseView {


2
3 void showProgress();
4
5 void hideProgress();
6
7 View getContentView();
8
9 }

BaseView.java hosted with ❤ by GitHub view raw


https://github.com/KucherenkoIhor/DelegationActivityTemplate/blob/master/app/src/main/java/com/ki/dat
/delegationactivitytemplate/base/BaseView.java

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

Open in app Get started

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

1 public abstract class BaseActivityDelegate<


2 V extends BaseView,
3 P extends BasePresenterImpl<V>> {
4
5 private Unbinder mUnBinder = null;
6
7 protected P mPresenter;
8
9 public void onCreate(P presenter) {
10 mPresenter = presenter;
11 mUnBinder = ButterKnife.bind(this, mPresenter.getView().getContentView());
12 }
13
14 public void onDestroy() {
15 mUnBinder.unbind();
16 }
17 }

BaseActivityDelegate.java hosted with ❤ by GitHub view raw


https://github.com/KucherenkoIhor/DelegationActivityTemplate/blob/master/app/src/main/java/com/ki/dat
/delegationactivitytemplate/base/BaseActivityDelegate.java

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.

1 public abstract class BaseDelegationActivity<


2 V extends BaseView,
3 P extends BasePresenterImpl<V>,
4 D extends BaseActivityDelegate<V, P>>
5 extends BaseActivity<P> {
6
7 protected D mDelegate;
8
9 @Override
10 protected void onCreate(@Nullable Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 mDelegate = instantiateDelegateInstance();
13 mDelegate.onCreate(mPresenter);
14 }
15
16 protected abstract D instantiateDelegateInstance();
17
18 @Override
https://proandroiddev.com/clean-your-activity-using-delegation-pattern-fcaafd82336d 6/12
20/06/2022, 08:46 Clean Your Activity Using Delegation Pattern | by Ihor Kucherenko | ProAndroidDev

19 protected void onDestroy() {


20 mDelegate.onDestroy(); Open in app Get started
21 super.onDestroy();
22 }
23 }

BaseDelegationActivity java hosted with ❤ by GitHub view raw


https://github.com/KucherenkoIhor/DelegationActivityTemplate/blob/master/app/src/main/java/com/ki/dat
/delegationactivitytemplate/base/BaseDelegationActivity.java

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:

1 public class NavigationDrawerDelegate extends BaseActivityDelegate<


2 NavigationDrawerContract.NavigationDrawerView,
3 NavigationDrawerPresenter> implements NavigationView.OnNavigationItemSelectedLi
4
5 @BindView(R.id.drawer_layout)
6 protected DrawerLayout mDrawerLayout;
7 @BindView(R.id.toolbar)
8 protected Toolbar mToolBar;
9 @BindView(R.id.nav_view)
10 protected NavigationView mNavigationView;
11
12 @Override
13 public void onCreate(NavigationDrawerPresenter presenter) {
14 super.onCreate(presenter);
15 configureDrawer();
16 }
17
18 private void configureDrawer() {
19 ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
20 mPresenter.getView().getActivity(),
21 mDrawerLayout,
22 mToolBar,
23 R.string.navigation_drawer_open,
24 R.string.navigation_drawer_close);
25 mPresenter.getView().getActivity().getSupportActionBar().setDisplayHomeAsUpEnab
26 mDrawerLayout.addDrawerListener(toggle);
27 toggle.syncState();
28 mNavigationView.setNavigationItemSelectedListener(this);
29 }
30
31 @Override
32 public boolean onNavigationItemSelected(@NonNull MenuItem item) {
33 switch (item.getItemId()) {
https://proandroiddev.com/clean-your-activity-using-delegation-pattern-fcaafd82336d 7/12
20/06/2022, 08:46 Clean Your Activity Using Delegation Pattern | by Ihor Kucherenko | ProAndroidDev
g
34 case R.id.nav_camera:
Open in app Get started
35 mPresenter.getView().openCamera();
36 break;
37 case R.id.nav_gallery:
38
39 break;
40 case R.id.nav_slideshow:
41
42 break;
43 case R.id.nav_manage:
44
45 break;
46 case R.id.nav_share:
47
48 break;
49 case R.id.nav_send:
50 mPresenter.doSomething();
51 break;
52 }
53 closeDrawer();
54 return true;
55 }
56
57 public boolean closeDrawer() {
58 if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
59 mDrawerLayout.closeDrawer(GravityCompat.START);
60 return true;
61 } else {
62 return false;
63 }
64 }
65 }

https://github.com/KucherenkoIhor/DelegationActivityTemplate/blob/master/app/src/main/java/com/ki/dat
/delegationactivitytemplate/delegation/NavigationDrawerDelegate.java

As we can see, all logic of Navigation Drawer is contained in


NavigationDrawerDelegate.

1 public class NavigationDrawerActivity extends BaseDelegationActivity<


2 NavigationDrawerContract.NavigationDrawerView,
3 NavigationDrawerPresenter,
4 NavigationDrawerDelegate>
5 implements NavigationDrawerContract.NavigationDrawerView {
6
7 @BindView(R.id.toolbar)
8 protected Toolbar mToolbar;
https://proandroiddev.com/clean-your-activity-using-delegation-pattern-fcaafd82336d 8/12
20/06/2022, 08:46 Clean Your Activity Using Delegation Pattern | by Ihor Kucherenko | ProAndroidDev

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

You might also like