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:
- My service populates
MyService.messages[]
with the messages to show and then startsMyScrollActivity
. MyScrollActivity
gets size ofMyService.messages[]
. If >1, startMyCyclerThread
(or AsyncTask, etc. - haven't figured out best way!)MyCyclerThread
loops throughMyService.messages[]
and setsMyScrollActivity
TextView withmessages[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 ati=0
and repeats the messages again.- Step 3 repeats until activity is taken down by the service or another activity is shown. The cycler-thread (or whatever - the loop) needs to die at this time.
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);