I'm beginner in Android, I read and see if use the static method in Util class for updating UI is not good for unit test. How do I avoid it in suitable way for maintaining code and unit test ?
Example:
class ActivityA {
private View view;
private MyListener myListener;
public void methodB() {
Util.callLogicB(this, view, myListener);
}
}
class ActivityB {
private View view;
private MyListener myListener;
public void methodC() {
Util.callLogicB(this, view, myListener);
}
}
class Util {
public static void callLogicB(Context context, View view, MyListener listener) {
// do something with view
}
}
I think the best way to implement this is to completely disconnect your views (your activity) from your logic class. This can be done using an interface.
Look at the code below to understand how an interface works. I created a mainActivity with a textView as a layout. The class LogicWithTextUpdate I created is capable of updating a textView inside your mainActivity without a direct reference to it.
public class MainActivity extends Activity implements MyViewListener {
//Your logic class, that is capable of updating a textview
LogicWithTextUpdate logicWithTextUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
//This is just to create a layout (with a textview inside)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//This creates the class using constructor (and set listener)
logicWithTextUpdate = new LogicWithTextUpdate(this);
//Update or execute your logic. This will update textview using interface MyViewListener (onUpdateTextListener)
//This can be executed using a button (Onclick...)
logicWithTextUpdate.doSomeLogic(2);
}
@Override
public void onUpdateTextListener(String text) {
//Only here code runs inside your activity class
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(text);
}
}
As you can see. MainActivity will update textview using a "listener" onUpdateTextListener. This is described inside a interface class:
public interface MyViewListener {
//to be implemented inside your mainActivity
public void onUpdateTextListener(String text);
}
Now I can implement a class that can do some logic and trigger the listener:
public class LogicWithTextUpdate {
MyViewListener myViewListener;
//Constructor here is used to set myViewListener (but you can use a setter like setMyViewListener)
public LogicWithTextUpdate(MyViewListener myViewListener) {
this.myViewListener = myViewListener;
}
public void doSomeLogic(int a) {
//Some logic
a = a * 2;
a = a + 1;
//Update text using listener. This will update mainActivity, because it is implementing MyViewListener
myViewListener.onUpdateTextListener(String.valueOf(a));
}
}
The method doSomeLogic is executed from your activity, but the logic is inside the class. After the logic is executed it triggers myViewListener.onUpdateTextListener that executes onUpdateTextListener inside your activity.
Everything is disconnect. You can change your views without any modification in LogicWithTextUpdate, or change the logic without modifications in Actitivy. Always a good practice.
AFAIK, it's good practice to exclusively use Utility classes for static, helper methods that generally return primitive and reference data as opposed to updating UI which should be done only in Activities. For instance, a sample Utility class would involve a series of methods such as:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With