user1568346
user1568346

Reputation: 29

After performing an action,how to delay the starting of a new action?

I have a variable and I have set it to 3. every time when button is clicked it is decremented and required action is performed. when it reaches 0 i want action should be display on the screen for a while and then it moves to new activity.

But problem is when it reaches 0 it does not hold for a while for the user to see the what action is performed it simply jumps to next page ... is there any way to do this?

the code I'm using is:

if(ch==0)
{
try {

        Thread.sleep(15*1000);
    startActivity(new Intent("com.example.quizproject.Menu"));
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

after using this code,the variable reaches 0 , it doesn't finishes the previous action rather pause then move to the next activity.

Any possible solution for this problem?

Upvotes: 0

Views: 593

Answers (2)

hasanghaforian
hasanghaforian

Reputation: 14022

You can create a class that extends Thread,for example MyThread:

public class MyThread extends Thread {

    SampleActivity sampleActivity;

    public MyThread(SampleActivity sampleActivity){
        this.sampleActivity = sampleActivity;
    }



        public void run() {
            try {
                sleep(15*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            Intent i = new Intent(this.sampleActivity, Asleep.class);
            sampleActivity.startActivity(i);
        }

}      

Edit:
Then use it when you want start Activity,here sampleActivity is first Activity(first question) and Asleep is Activity that appears after 15 seconds(second question).So for example in click listener of a button in sampleActivity do like this:

MyThread mt = new MyThread(this);
mt.start();

Upvotes: 2

TheBlueCat
TheBlueCat

Reputation: 1195

Why do you want the thread to sleep? You will look up the main thread and the user will not go to the Intent as the thread(sleep) is before the Intent.

That's why.

so:

while (ch <=0) {
Intent i = new Intent (this, menu.class);
startActivity(i);
}

Upvotes: 0

Related Questions