MVP + Android Architecture Components = ❤ Few days ago, during Google I/O was presented . We decided to combine these components with MPV Architecture. So, let’s see what we got in a result. Android Architecture Components One of the common problems working with MPV Architecture is while changing screen orientation. There are some solutions for that (Loaders, retainInstance, Mosby, and so on). to keep Presenter’s state Moxy One of those Architecture Components presented during the I/O was a ViewModel . “ ” — from official documentation. The ViewModel class is designed to store and manage UI-related data so that the data survives configuration changes such as screen rotations. It gives us an opportunity to save the object during screen orientation change. The other component is which we will use. Lifecycle , “ ” - from official documentation. The android.arch.lifecycle package provides classes and interfaces that let you build lifecycle-aware components — which are components that can automatically adjust their behavior based on the current lifecycle of an activity or fragment. 1. Before orientation change 2. After orientation change From the beginning let’s design our and . Let’s create a Presenter View Contract Interface. BaseContract { public interface **interface** View { } **interface** Presenter<V **extends** BaseContract.View> { **void** attachLifecycle(Lifecycle lifecycle); **void** detachLifecycle(Lifecycle lifecycle); **void** attachView(V view); **void** detachView(); V getView(); **boolean** isViewAttached(); **void** onPresenterDestroy(); } } After this, we need to create that implements our . Presenter Presenter Contract BasePresenter<V BaseContract.View> LifecycleObserver, BaseContract.Presenter<V> { public abstract class extends implements **private** V **view**; @Override **final public** V getView() { **return view**; } @Override **final public void** attachLifecycle(Lifecycle lifecycle) { lifecycle.addObserver(**this**); } @Override **final public void** detachLifecycle(Lifecycle lifecycle) { lifecycle.removeObserver(**this**); } @Override **final public void** attachView(V view) { **this**.**view** \= view; } @Override **final public void** detachView() { **view** \= **null**; } @Override **final public boolean** isViewAttached() { **return view** != **null**; } } So, we have a , which attaches and detaches the and . Presenter View Lifecycle Observer With the next step, let’s create our . Here we have a problem: the presenter will be every time after the orientation change. Activity recreating Let’s go ahead. The is automatically retained during configuration changes so the data it holds is immediately available to the next activity or fragment instance. This will help us not to the Presenter every time. ViewModel initialize For that, we will create the , which extends from . It will receive and return our presenter’s object. also has method. If the activity is re-created, it receives the same instance that was created by the previous activity. When the owner activity is finished, the Framework calls method so that it can clean up resources. Class ViewModel ViewModel onCleared() ViewModel ViewModel ’s onCleared() BaseViewModel<V BaseContract.View, P BaseContract.Presenter<V>> ViewModel { public final class extends extends extends **private** P **presenter**; **void** setPresenter(P presenter) { **if** (**this**.**presenter** \== **null**) { **this**.**presenter** \= presenter; } } P getPresenter() { **return this**.**presenter**; } @Override **protected void** onCleared() { **super**.onCleared(); **presenter**.onPresenterDestroy(); **presenter** \= **null**; } } In our next step we will create the instance inside the Activity. BaseViewModel Also we must give the to the . So we need And the Activity must implement to the Lifecycle Presenter LifecycleRegistry. LifecycleRegistryOwner. BaseActivity<V BaseContract.View, P BaseContract.Presenter<V>> AppCompatActivity BaseContract.View, LifecycleRegistryOwner { public abstract class extends extends extends implements **private final** LifecycleRegistry **lifecycleRegistry** \= **new** LifecycleRegistry(**this**); **protected** P **presenter**; @CallSuper @Override **protected void** onCreate(@Nullable Bundle savedInstanceState) { **super**.onCreate(savedInstanceState); BaseViewModel<V, P> viewModel = ViewModelProviders._of_(**this**).get(BaseViewModel.**class**); **if** (viewModel.getPresenter() == **null**) { viewModel.setPresenter(initPresenter()); } **presenter** \= viewModel.getPresenter(); **presenter**.attachLifecycle(getLifecycle()); **presenter**.attachView((V) **this**); } @Override **public** LifecycleRegistry getLifecycle() { **return lifecycleRegistry**; } @CallSuper @Override **protected void** onDestroy() { **super**.onDestroy(); **presenter**.detachLifecycle(getLifecycle()); **presenter**.detachView(); } **protected abstract** P initPresenter(); } At the first time creating the Activity, we must create a Presenter and give it to the . The next time when the activity recreates, we just reuse our from the . ViewModel Presenter ViewModel At the first time creating Presenter, we must attach and . For avoiding memory leak, we also need to detach and in . Lifecycle View Lifecycle View onDestroy() As a bonus, we also have inside presenter. Activity Lifecycle events @OnLifecycleEvent(value = Lifecycle.Event. ) onCreate() { ON_CREATE protected void } For the complete puzzle, something lacks. It is a state of . Views For that, we must have in our , and stateBundle in the , which will keep and return our state. Bundle getStateBundle() BaseContract Bundle Presenter View The final Presenters is like that: BasePresenter<V BaseContract.View> LifecycleObserver, BaseContract.Presenter<V> { public abstract class extends implements **private** Bundle **stateBundle**; **private** V **view**; @Override **final public** V getView() { **return view**; } @Override **final public void** attachLifecycle(Lifecycle lifecycle) { lifecycle.addObserver(**this**); } @Override **final public void** detachLifecycle(Lifecycle lifecycle) { lifecycle.removeObserver(**this**); } @Override **final public void** attachView(V view) { **this**.**view** \= view; } @Override **final public void** detachView() { **view** \= **null**; } @Override **final public boolean** isViewAttached() { **return view** != **null**; } @Override **final public** Bundle getStateBundle() { **return stateBundle** \== **null** ? **stateBundle** \= **new** Bundle() : **stateBundle**; } @CallSuper @Override **public void** onPresenterDestroy() { **if** (**stateBundle** != **null** && !**stateBundle**.isEmpty()) { **stateBundle**.clear(); } } } Thanks for reading this article. You can find full code there . Let’s become friends on Twitter , Github and Facebook . If you enjoyed the writings then please use the ❤ heart below to recommend this article so that others can see it. We will also love to hear your comments and suggestions :) Thanks.