epicness42
epicness42

Reputation: 1137

How to Stop a Loop at the Press of a Button

I want the onClick method not only to create a new activity to the new page, but also to trigger the end of the loop, so that if someone clicks the background of the splash screen, the new screen doesn't reload after the loop stops.

Here is my code,

package clouds.clouds;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class splash extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
     Thread logotimer = new Thread() {


            @Override
            public void run(){
                try{
                    int logotimer = 0;
                    while(logotimer <5000) {
                        sleep(100);
                        logotimer = logotimer +100;

                    }
                    startActivity(new Intent("clouds.clouds.SPLASH"));
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally{
                    finish();
                }

            }



        };
      logotimer.start();


}



    public void onClickz(View v){}
    public void speed2 (View v){

        startActivity(new Intent("clouds.clouds.BUTTONZ"));
    }



}

Any suggestions?

Upvotes: 0

Views: 2389

Answers (4)

epicness42
epicness42

Reputation: 1137

package clouds.clouds;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class splash extends Activity {
Thread logotimer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

     logotimer = new Thread();
     Thread logotimer = new Thread() {

            @Override
            public void run(){
                try{
                    int logotimer = 0;
                    while(logotimer <5000) {
                        sleep(100);
                        logotimer = logotimer +100;
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally{
                    finish();
                }
                startActivity(new Intent("clouds.clouds.SPLASH"));
            }



        };
      logotimer.start();


}


public void onClickz (View v){}
public void speed2 (View v){
    logotimer.interrupt();
    startActivity(new Intent("clouds.clouds.BUTTONZ"));
}






}

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180868

Add a volatile boolean variable to your class (call it cancelled). Set it to true when the button is clicked, and check for cancelled == false in your while condition.

public class splash extends Activity {

    volatile bool cancelled = false;
...

protected void onCancel(...)
{
    cancelled = true;

...

while(!cancelled && logotimer <5000) {
...

Upvotes: 3

Aaron
Aaron

Reputation: 11693

boolean exit = false;
int logotimer = 0;
while(logotimer <5000 && exit != false) {
  sleep(100);
  logotimer = logotimer +100;
  // value = ???    
  if(logotimer == value) {
      exit = true;
  }
}

Upvotes: 0

wnafee
wnafee

Reputation: 2146

Call logotimer.interrupt() in your onClick() method. This should cause an InterruptedException in your thread which you should handle by doing nothing (or whatever else you want to do when you interrupt your thread)

Upvotes: 3

Related Questions