Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The suitable way for avoiding static method (in Util class maybe) which use for updating UI

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
    }
}
like image 369
Hien Nguyen Avatar asked Jun 15 '17 02:06

Hien Nguyen


2 Answers

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.

like image 185
Adriano Moutinho Avatar answered Oct 07 '22 13:10

Adriano Moutinho


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:

  • A method that builds a URL object out of a HTTP request URL in String format
  • A method that returns readable JSON data after successfully connecting to the HTTP server
  • A method that extracts the JSON data and stores them in a returnable ArrayList
  • A public, static method that invoked all of the above to ultimately return the ArrayList of data to an Activity via its background thread to later render UI via its UI thread
like image 2
DaveNOTDavid Avatar answered Oct 07 '22 13:10

DaveNOTDavid