I would like to create a CountdownTimer
which will trigger events that will update the UI (trigger popup, start an animation, etc.).
I wonder how to do this clean, here are my hypothesis and why :
EventCountdownTimer
. I could then benefit the use of LifecycleObserver
, but I wonder how to communicate the information back to the activity (I tried extending CountdownTimer
and using it in the activity but I have an error and can't get it to compile)Activity
itself, it's the simplest but I'm not sure it belongs there as it isn't a UI component and I can't benefit the LifecycleObserver
ViewModel
. I thought as it's activity related and the CountdownTimer
is kinda logic data, it should go in here, but that means also watching the lifecycle of the activity, and holding any Activity
related field within ViewModel
is bad practice.What's the best option according to you? And why?
Caution: A ViewModel must never reference a view, Lifecycle , or any class that may hold a reference to the activity context. ViewModel objects are designed to outlive specific instantiations of views or LifecycleOwners .
onFinish() - After finish ticking, if you want to call any methods or callbacks we can do in onFinish(). start() - It is used to call countdown timer. cancel() - It is used to cancel countdown timer. This example demonstrates how to integrate countdown timer with textview.
android.os.CountDownTimer. Schedule a countdown until a time in the future, with regular notifications on intervals along the way. Example of showing a 30 second countdown in a text field: Kotlin Java.
A CountDownTimer is an accurate timer that can be used for a website or blog to display the count down to any special event, such as a birthday or anniversary. Likewise, here let’s create an Android App to learn how to create a simple Countdown App.
The CountDownTimer class provides onFinish () and onTick () methods. Step 1 Create a project like this: Select "File" -> "New Project" -> "Android Application". Step 2 Create an XML file and write this:
Use Up/Down Arrow keys to increase or decrease volume. PulseCountDown in Android is an alternative to CountDownTimer. It is very easy to implement PulseCountDown instead of CountDownTimer because PulseCountDown provides a default layout with some beautiful animations.
Example of showing a 50-second countdown in a text field: textView.setText (f.format (hour) + “:” + f.format (min) + “:” + f.format (sec));
In a MVVM pattern you could have a LiveData observable in your ViewModel which will be observed by the UI and upon value change you update the UI accordingly. How that observable changes value, that is your business logic and all of it should be in your ViewModel or in separate components that will be used by the ViewModel to update the observable state.
This will allow you to separate the UI from the business logic being your observable the bridge of communication between both, without the ViewModel having any knowledge of whats going on in the UI. In simple words it only executes what it is told to execute and updates a variable that is being observed, what then happens in the UI is the UI responsibility and with this you have reached a clear separation of concerns.
A separate component "EventCountdownTimer"
In my opinion, this is the best implementation that you might have in your case. For communicating information back to your activity, you might consider having an interface like the following.
public interface TimerListener {
void onTimerResponse(String response);
}
Modify your EventCountdownTimer
to have a constructor which takes TimerListener
as a parameter and override the onTimerResponse
method in your activity. Now from your EventCountdownTimer
, when you are trying to communicate with your activity along with a message, for example, you might just call the function onTimerResponse(msgToDeliver)
.
Hence your EventCountdownTimer
should look something like this.
public class EventCountdownTimer {
public static Context context;
public static TimerListener listener;
public EventCountdownTimer(Context context, TimerListener listener) {
this.context = context;
this.listener = listener;
}
public startCountdown() {
// Start the count down here
// ... Other code
// When its time to post some update to your activity
listener.onTimerResponse(msgToDeliver);
}
}
And from your activity, initialize the EventCountdownTimer
like the following.
EventCountdownTimer timer = new EventCountdownTimer(this, new TimerListener() {
@Override
public void onTimerResponse(String message) {
// Do something with the message data
// Update your UI maybe
}
});
I think you have provided good reasons already for not going for other options that you have mentioned.
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