I have an activity that gets started by a background service in order to show scrolling message(s) that the service receives from a server. It's possible to have just one scrolling message or a set of multiple to scroll, one at a time.
My question is what is the ideal way to manage cycling the scrolling of multiple messages, and multiple sets of multiple messages? I've tried my cycler loop within worker threads, AsyncTask, etc.
The Details:
My scroll activity receives the message(s) to show, as an array of strings available for it to access from the service's scope. Say, for example, the service starts the activity in order to scroll several messages, one after another:
MyService.messages[]
with the messages to show and then starts MyScrollActivity
.MyScrollActivity
gets size of MyService.messages[]
. If >1, start MyCyclerThread
(or AsyncTask, etc. - haven't figured out best way!)MyCyclerThread
loops through MyService.messages[]
and sets MyScrollActivity
TextView with messages[i]
and animates across screen once, one message-iteration at a time (one message scrolls across, then the next, etc. - at end of loop, it starts over at i=0
and repeats the messages again.I want that particular worker thread (that's doing the looping) to die and never come back whenever its activity stops. But for some reason, whenever the activity is restarted (say a new batch of messages comes in to display), the old cycler/worker thread resurrects, along with a new one for the new messages.
I've tried adding Thread.currentThread().interrupt()
to the activity's onPause
and onDestroy
methods, and I see in my logging that the thread does get interrupted. But like I said, it resurrects (even with the same ID it had before) and starts its loop again. This means for however many 'X' times I start my activity, I also get 'X-1' number of threads running.
I toyed with Activity single-instance XML, various "isThreadABCrunning" flags, etc. I've even tried using an AsyncTask for my cycler loop. I'm running out of ideas.
Help, I'm beginner/intermediate in Android... I know just enough to make trouble, apparently. Is there something I'm missing about threading or activities? Is it perhaps something as simple as handling my activity differently?
I'd appreciate a 30,000 foot overview, and then I can provide code samples, as needed (I've gone through too many iterations to pin down one to post here for now), so sorry if I'm treading close to violating a post's best-practice.
Update: animation code
slide = new TranslateAnimation(dm.widthPixels, -params.width, 0, 0);
slide.setDuration(animDuration);
slide.setRepeatCount(repititions);
slide.setRepeatMode(Animation.RESTART);
slide.setInterpolator(new LinearInterpolator());
slide.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
msgIsScrolling = true;
}
@Override
public void onAnimationEnd(Animation animation) {
msgIsScrolling = false;
}
@Override
public void onAnimationRepeat(Animation animation) {
//nothing to do
}
});
textView_message.startAnimation(slide);
My gut reaction is that "multithreaded" solutions are the wrong way to approach this problem. My first thought is:
Animator.AnimatorListener
's onAnimationEnd()
method to re-trigger your animation.Essentially, you'd have some method inside the activity that takes a message and starts it animating. You'd call this method both when your activity is first shown and whenever a message is done scrolling.
Since this all lives inside the activity itself, there shouldn't need to be any cleanup when the activity dies.