4

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?

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132

3 Answers3

6

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 
PieterAelse
  • 3,578
  • 2
  • 23
  • 33
  • Following your example i did `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()); new Handler().postDelayed(new Runnable() { public void run () { // Do delayed stuff! startActivity(userProfileDisplayIntent); } }, 5000L); } }});` It works.But just like Darkhogg metioned above,it is best to wait for persistence completion – Ojonugwa Jude Ochalifu Feb 11 '14 at 00:48
  • If you want to execute something after the task finished I suggest you to look into using [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html). With it's 'callbacks' onPreExecute and onPostExecute you can do something before and after the thread-work is done. The doInBackground method is where your work can be done, it runs on a different thread. An example of using AsyncTask can be found here: http://stackoverflow.com/a/9671602/1534666 – PieterAelse Feb 11 '14 at 11:00
2

Start activity in

onPostExecute()

of

EndpointsTask

Your EndPoints task should look like that

public final class EndpointsTask extends AsyncTask {

    private final Intent mUserProfileDisplayIntent;
    private final WeakReference<Activity> mActivityReference;

    EndpointsTask(final Activity activity, final Intent userProfileDisplayIntent) {
        mActivityReference = new WeakReference<Activity>(activity);
        mUserProfileDisplayIntent = userProfileDisplayIntent;
    }

    @Override
    protected void onPostExecute(Void result) {
        final Activity activity = mActivityReference.get();
        if (activity != null) {
            startActivity(mUserProfileDisplayIntent);
        }
    }
}

And then

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.userDetailsCaptureButton) {
            // make sure only one task can be launched
            if (endPointsTask == null || endPointsTask.getStatus() != AsyncTask.Status.RUNNING) {
                 endPointsTask = new EndpointsTask(YourActivity.this);
                 endPointsTask.execute();
            }
        }
        startActivity(userProfileDisplayIntent);
    }

// always cancel AsyncTasks in onStop(). You don't want it to launch anything when minimized, do you?

@Override
protected void onStop() {
    super.onStop();
    if (endPointsTask != null && endPointsTask.getStatus() == AsyncTask.Status.RUNNING) {
        endPointsTask.cancel();
    }
}

Don't pass a Context as a parameter to any thread since it can cause leaks. This means as well that inner classes should be static. Store Context as a WeakReference (use mActivityReference for Context in current example)

Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
  • @OjonugwaOchalifu what do you mean you can't? You can generate anything you want. I'm sure there is a way. – Yaroslav Mytkalyk Feb 10 '14 at 15:36
  • I meant modifying it would mean i would have to go and change a lot of other things. – Ojonugwa Jude Ochalifu Feb 10 '14 at 15:42
  • @OjonugwaOchalifu AsyncTask should perform a sort action and deliver a result. If it's hard to modify maybe it's badly engineered. If you don't need it to launch anything or launch different things consider making it abstract and create phew tasks that implement the abstract launch method or something that suites your needs. – Yaroslav Mytkalyk Feb 10 '14 at 16:00
  • Am not saying it's impossible to modify,i am new to this and the code so if i change the code now, it will be hard for me to follow what am doing. – Ojonugwa Jude Ochalifu Feb 11 '14 at 00:47
1

Try it with a Handler and postDelayed-method inside onClick

A.D.
  • 1,412
  • 2
  • 19
  • 37