Presenters via Loaders in Open Event Organizer Android App
Open Event Organizer‘s App design follows Model View Presenter (MVP) architecture which facilitates heavy unit testing of the app. In this design pattern, each fragment/activity implements a view interface which uses a presenter interface to interact with a model interface. The presenter contains most of the data of the view. So it is very important to restore presenters after configuration changes like rotation. As on rotation, the complete activity is re-created hence all the fields are destroyed and as a result, everything is re-generated resulting in state loss on configuration change which is unexpected. Open Event Organizer App uses the loader to store/provide presenters to the activity/fragment. Loader survives configuration changes. The idea of using the loader to provide presenter is taken from Antonio Gutierrez’s blog on “Presenters surviving orientation changes with loaders“.
The first thing to do is make a PresenterLoader<T> class extending Loader<T> where T is your presenter’s base interface. The PresenterLoader class in the app looks like:
public class PresenterLoader<T extends IBasePresenter> extends Loader<T> { private T presenter; ... @Override protected void onStartLoading() { super.onStartLoading(); deliverResult(presenter); } @Override protected void onReset() { super.onReset(); presenter.detach(); presenter = null; } public T getPresenter() { return presenter; } }
The methods are pretty clear from the names itself. Once this is done, now you are ready to use this loader in for your fragment/activity. Creating a BaseFragment or BaseActivity will be clever as then you don’t have to add same logic everywhere. We will take a use case of an activity. A loader has a unique id by which it is saved in the app. Use unique id for each fragment/activity. Using the id, the loader is obtained in the app.
Loader<P> loader = getSupportLoaderManager().getLoader(getLoaderId());
When creating for the first time, the loader is set up with the loader callbacks where we actually set a presenter logic. In the Organizer App, we are using dagger dependency injection for injecting presenter in the app for the first time. If you are not using the dagger, you should create PresenterFactory class containing create method for the presenter. And pass the PresenterFactory object to the PresenterLoader in onCreateLoader. In this case, we are using dagger so it simplifies to this:
getSupportLoaderManager().initLoader(getLoaderId(), null, new LoaderManager.LoaderCallbacks<P>() { @Override public Loader<P> onCreateLoader(int id, Bundle args) { return new PresenterLoader<>(BaseActivity.this, getPresenterProvider().get()); } @Override public void onLoadFinished(Loader<P> loader, P presenter) { BaseActivity.this.presenter = presenter; } @Override public void onLoaderReset(Loader<P> loader) { BaseActivity.this.presenter = null; } });
getPresenterProvider method returns Lazy<Presenter> provider to ensure single presenter creation in the activity/fragment. The lifecycle to setup PresenterLoader in activity is onCreate and in the fragment is onActivityCreated. Use presenter field from next lifecycle that is start. If the presenter is used before the start, it creates null pointer exception. For example, if implementing with the BaseFragment, setup loader in onActivityCreated method.
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Loader<P> loader = getSupportLoaderManager().getLoader(getLoaderId()); if (loader == null) { initLoader(); } else { presenter = ((PresenterLoader<P>) loader).getPresenter(); } }
Make sure that your base interface implements some of the basic methods. For example, onDetach, onAttach etc. getLoaderId method must be implemented in each fragment/activity using loaders. The method returns unique id for each fragment/activity. In Organizer App, the method returns layout id of the fragment/activity as a unique id.
Using the loader approach to store/restore presenters helps in surviving their instances in configuration changes in the app. Hence improves the performance.
Links:
Antonio Gutierrez’s blog post about Presenter surviving orientation changes with Loaders in Android
Android Documentation for Loaders
You must be logged in to post a comment.