I have a loading screen, I want to display the loadingMessage1 for 3 secs than shows loading message 2. I only want loading message 2 to appear once, but when I tried to do it, loading message 2 kept getting appended in an endless loop.
So i tried put a count variable to increases after every loading message 2 append, but it doesnt work. where am i going wrong, is there another solution to this?
final TextView loadingMessage1 = (TextView)this.findViewById(R.id.loadingMessage1);
int count = 0;
final Handler handler = new Handler();
if (count == 0){
handler.post(new Runnable(){
@Override
public void run(){
loadingMessage1.append("Loading Message 2");
handler.postDelayed(this, 3*1000L);
}
});
count++;
}
You are making a mistake on the following line:
Here you send to this
Runnableobject to the handler while handlingthis. That means when you fist execute theRunnable, on the end you add it again to the handler’s message loop. Thus you obtained an infinite loop. Something of the like will solve your problem:EDIT:
To make all this stuff flexible, do the following:
and then somewhere in your code:
and so on. The trick is not to reference
thisas you did before, because it creates an infinite loop.