• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Delay function execution and possibility to cancel its execution

Fr3eZer

Lurker
Jul 5, 2014
1
0
I have a animated loader in my application right now.

public void startLoader() {
Runnable task = new Runnable() {
@Override
public void run() {
lock = true;
loader.setVisibility(View.VISIBLE);
}
};
runOnUiThread(task);
}

public void stopLoader() {
Runnable task = new Runnable() {
@Override
public void run() {
lock = false;
loader.setVisibility(View.GONE);
}
};
runOnUiThread(task);
}
Now I want it to work like this:

stopLoader should have an delayed execution of like 1000ms, so it dissapears 1 second after stopLoader is called.
but if the startLoader is called within that 1000ms the stopLoader timer would be cancelled.

What is the best way to implement this?
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones