I am somewhat new to Android
, and I am writing an app. I am getting to the point where I am starting to more thoroughly test my code, and therefore, I would like to implement the MVP design strategy since it adds more testable layers to the code. One of the supposed benefits of using MVP that I can not seem to understand is how it helps with running AsyncTasks
as they are performed dynamically. Since you want to avoid any Android
specific components in your Presenter
class, how are you supposed to reference the Activity
that utilizes the AsyncTasks
? Tutorials about MVP show the Presenter
object having methods that take in an Activity
as a parameter and return to it; however, if your AsyncTask
takes a long time and your Activity
has been destroyed through something such as rotation change, how do you return to the proper Activity
? I currently store my AsyncTask
in a Fragment
so that it is saved on Orientation Change. I am having a hard time finding a workaround that implements the MVP practice.
To answer your question, there isn't much you can do to avoid passing Android classes to your Presenter class. But instead of passing the Android object as a parameter, add a method to your View class that returns it (e.g. getActivity()
).
That said, I strongly suggest you use a Loader
instead of an AsyncTask
. Loaders
were designed specifically for your use-case. They can also run in the background but their lifecycle is tied to the lifecycle of an Activity
or Fragment
.
If you switch to Loaders
, add a method like getLoaderManager()
to your View interface.