How to Pause/Sleep Thread or Process in Android

How to pause / sleep thread or process in Android?

One solution to this problem is to use the Handler.postDelayed() method. Some Google training materials suggest the same solution.

@Override
public void onClick(View v) {
my_button.setBackgroundResource(R.drawable.icon);

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
my_button.setBackgroundResource(R.drawable.defaultcard);
}
}, 2000);
}

However, some have pointed out that the solution above causes a memory leak because it uses a non-static inner and anonymous class which implicitly holds a reference to its outer class, the activity. This is a problem when the activity context is garbage collected.

A more complex solution that avoids the memory leak subclasses the Handler and Runnable with static inner classes inside the activity since static inner classes do not hold an implicit reference to their outer class:

private static class MyHandler extends Handler {}
private final MyHandler mHandler = new MyHandler();

public static class MyRunnable implements Runnable {
private final WeakReference<Activity> mActivity;

public MyRunnable(Activity activity) {
mActivity = new WeakReference<>(activity);
}

@Override
public void run() {
Activity activity = mActivity.get();
if (activity != null) {
Button btn = (Button) activity.findViewById(R.id.button);
btn.setBackgroundResource(R.drawable.defaultcard);
}
}
}

private MyRunnable mRunnable = new MyRunnable(this);

public void onClick(View view) {
my_button.setBackgroundResource(R.drawable.icon);

// Execute the Runnable in 2 seconds
mHandler.postDelayed(mRunnable, 2000);
}

Note that the Runnable uses a WeakReference to the Activity, which is necessary in a static class that needs access to the UI.

Wait for 5 seconds

just add one-liner with lambda

(new Handler()).postDelayed(this::yourMethod, 5000);

edit for clarification: yourMethod refers to the method which you want to execute after 5000 milliseconds.

How to insert a sleep/pause before the next method is called in android?

public void onClick(View v) {
v.postDelayed(new Runnable() {
public void run() {
// do your delayed work
}
}, DELAY_PERIOD);
}

where DELAY_PERIOD is the delay period in milliseconds. Here, onClick() is in your OnClickListener implementation, or is the method pointed to by android:onClick in your layout, or is pointed to by the data binding framework.

Android sleep() without blocking UI

You can use postDelayed() method like this:

handler=new Handler();
Runnable r=new Runnable() {
public void run() {
//what ever you do here will be done after 3 seconds delay.
}
};
handler.postDelayed(r, 3000);

How to delay the execution of code without using a new thread in Android

Handler is what you use to delay something in your thread. In the following case, the handler makes your UI TextView disappear after waiting for ten seconds;

private void makeTextViewDisappear(){
yourTV.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
yourTV.setVisibility(View.INVISIBLE);
// OR yourTV.setVisibility(View.GONE) to reclaim the space
}
}, 10000);
}

As you can see, there is no new thread.

How can i pause program execution in the scenario given in description?

You can use Handler to achieve this. Using postdelayed method you can do this. Below is the code to do this

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button.setText("xyz"); // text changed to xyz
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
button.setText("abc"); //text changed to abc after 1 second.
}
},1000);
}
});

Thread.sleep of main thread is freezing the app, android

Using Thread.sleep() from the main thread caused the app to freeze, Use animation callbacks as below:

fadeIn = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
fadeIn.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {

}
});


Related Topics



Leave a reply



Submit