q126y
q126y

Reputation: 1671

Sequential exeution of operations that modify UI

Actual work is being done in Asynctask, the AsyncTask returns the results. Users need visual cue as to what happened behind the scenes, which involves multiple independent UI modifications to be done, sequentially. UI modifications involve animations so the next job in queue has to wait for the animation of current job to finish. [the sequence is not fixed, but varies according to the result received by AsyncTask.]

All heavy lifting like network access is done by AsyncTask, I am looking for a better way to modify the UI sequentially.

My Current Approach: Currently I am using https://github.com/yigit/android-priority-jobqueue

When a UI modification finishes, it sends out event on eventbus, and jobManager is started[it is stopped when a job's onRun is called].

I am currently posting on UI handler inside onRun of the Job. It works, but the library is designed for writing to disks and long running operations and not UI operations. Is there a better way to do it?

Upvotes: 0

Views: 155

Answers (2)

Kushan
Kushan

Reputation: 5984

put your UI modification inside the onPostExecute method of your AsyncTask, it runs on the UI thread by default so you can update it from there after your whole loading is finished in doInBackground.

If you immediately want to do this inside doInBackground, use the following method inside that method.

 runOnUiThread(new Runnable(){

  public void run(){

  //your ui updations here
  }

});

Set an animation Listener to each animation and use onAnimationEnd to trigger next animation here:

fadeInAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {
   //trigger next animation here to make it sequential
}

@Override
public void onAnimationRepeat(Animation animation) {

}
});

This will help batching and maintaining sequences of animations

Upvotes: -1

dipdipdip
dipdipdip

Reputation: 2566

I highly recommend you to use RxJava!

  • You will get rid of the AsyncTask
  • Your Logik seems a bit more difficult and you want to jump between different Threads. RxJava will help you a lot with it.

Hint: You also should use Retrolambda to make RxJava your Code cleaner.

Upvotes: -1

Related Questions