1

I am working on a timer app in android studio for learning purposes. and I am looking for a way to make my code a bit more concise and tidy. For Updating the UI in my thread I used Runnable methods out side of the code block and just call them from the inside. but is there a way to use fewer lines of code to make UI commands inside the thread for something like this:

 bt_start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            running = true;
            thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            getCurrentTime();
                            handler.post(setTV);
                            Thread.sleep(1000);
                            if (running == false) {
                                thread.sleep(3000);
                                handler.post(stopTv);
                                break;
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            thread.start();
        }
    });

and these two are the methods:

    final Runnable setTV = new Runnable() {
        @Override
        public void run() {
            tv.setText("" + hr + ":" + min + ":" + sec);
        }
    };

    final Runnable stopTv = new Runnable() {
        @Override
        public void run() {
            tv.setText("Time Stopped!!!");
        }
    };
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Alex_89
  • 121
  • 6
  • `I am working on a timer app in android studio` yip, you're doing this _in_ android studio, but your code would be exactly the same in any other IDE as well, so there's no need to mention it :) – a_local_nobody Jan 30 '21 at 13:30
  • Also have a look at [this](https://stackoverflow.com/questions/4597690/how-to-set-timer-in-android) and/or [this](https://stackoverflow.com/questions/1877417/how-to-set-a-timer-in-android) posts for alternative ways to create timers. – gthanop Jan 30 '21 at 13:35
  • you don't need to create another thread to make timer handler will do, or if you subclass textview for like creating a timertextview it is the best , i will encapsulate all timer logic in one class giving to better encapsulation , separation of concern and you will be following single reponsibility principle , also you will not have to create any thread , every view has a handler you can access by just saying `getHandler()` – Abhinav Chauhan Jan 30 '21 at 14:26

1 Answers1

0

Creating threads in onClick is a bad practice, because you'll create a new thread on each click. And each thread puts a load on your CPU. Better use a single HandlerThread and post Runnables to it. Also use Java lambdas to make your code cleaner.

private HandlerThread bgThread;
private Handler bgHandler;
private boolean running = true;

@Override
public void onCreate() {
    super.onCreate();
    bgThread = new HandlerThread("timer");
    bgThread.start();
    bgHandler = new Handler(bgThread.getLooper());
}

...
bt_start.setOnClickListener(new View.OnClickListener() {
    bgHandler.post(() -> {
        while (running) {
            handler.post(setTV); // handler is a Handler of the main thread I suppose
            Thread.sleep(1000);
        }
        Thread.sleep(3000);
        handler.post(stopTv);
    });
});
...

@Override
public void onDestroy() {
    super.onDestroy();
    running = false;
    bgThread.quitSafely(); // releasing resources in onDestroy() is a good practice
    bgThread = null;
    bgHandler = null;
}
alexal1
  • 394
  • 3
  • 7