I have a value inside my app which constantly increases, the handler which does that is in a static method inside my "EventHandlerClass.java". I now want to show this value on my TextView inside my MainActivity.
Here is my static method with the handler:
public static void pointsCounter() {
handler = new Handler(Looper.getMainLooper());
runnable = new Runnable() {
public void run() {
points = points + 5;
String pointMsg = "Points: " + points;
MainActivity.coinsTextView.setText(pointMsg);
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(runnable, 1000);
}
This pointsCounter Method gets called from another static method inside the EventHandlerClass.java.
It increases the points value +5 every second and I want it to get displayed in a TextView. What is the right way to do it? Because when I try it this way I have to make the coinsTextView in the MainActivity static, we all know you cannot reference non static variable from static context. When I do so it tells me "Do not place Android context classes in static fields; this is a memory leak". So my question is how can I update my UI element from the static method without risking a memory leak? What is the right way to do it?