Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to Thread.sleep method in Android

Tags:

java

android

I have this button that when clicked saves the details entered in my textfields to Google App Engine, right after the call to that onClickListener, i have an intent that starts a new activity, which displays the details I just entered. Here is the code for this:

submitButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v.getId() == R.id.userDetailsCaptureButton) {
                new EndpointsTask().execute(getApplicationContext());
            }

            startActivity(userProfileDisplayIntent);
        }
    });

Now i want to be able to wait for a couple of seconds before making a call to startActivity after new EnpointsTask().execute(getApplicationContext) has been called. I read that using Thread.sleep causes the UI to freeze and as such isn't best suited for this. What other options are there?

like image 796
Ojonugwa Jude Ochalifu Avatar asked Jan 11 '23 05:01

Ojonugwa Jude Ochalifu


1 Answers

The solution is to use a handler with a runnable and use of the method 'postDelayed'. Example:

new Handler().postDelayed(new Runnable() {
    public void run () {
        // Do delayed stuff!
    }
}, 5000L); //5 seconds delay 
like image 107
PieterAelse Avatar answered Jan 22 '23 10:01

PieterAelse