zoonsf zoonsf - 9 days ago 12
Android Question

Android MVP - Should avoid using R.string references in presenter?

In an attempt to entirely decouple the Android SDK from my presenter classes, I'm trying to figure out the best way to avoid accessing resource IDs which we normally use R for. I thought I could just create an interface to access things like string resources, but I still need IDs to reference the strings. If I were to do something like...

public class Presenter {
private MyView view = ...;
private MyResources resources = ...;

public void initializeView() {
view.setLabel(resources.getString(LABEL_RES_ID);
}
}


I still have to have
LABEL_RES_ID
and then map it to
R.string.label
in my resources bridge. It's cool because I could swap it out when unit testing with something else, but I don't want to manage another mapping to the string value.

If I give up and just use the R.string values, my presenter is bound to my view again. That's not ideal? Is there an easier solution that people use to get around this in order to keep them out of the presenter. I don't want to manage strings in a way outside of what Android provides, because I still want to throw them in layout files and get the benefit of internationalization, etc. I want to make a dumb unit test that can work with this presenter without having to have the Android SDK generate the R.java files. Is this too much to ask?

Answer

I consider that there's no reason to call any android code in Presenter (But you always can do it).

So in your case:

View / activity onCreate() calls -> presenter.onCreate();

Presenter onCreate() calls -> view.setTextLabel() or whatever you want in the view.

Always decouple Android SDK from presenters.

In Github, you can find some examples about MVP: