• 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
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?
 
Back
Top Bottom