0

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?

epicness42
  • 1,137
  • 11
  • 12
  • http://stackoverflow.com/questions/2161750/android-controlling-a-task-with-timer-and-timertask – Alex Lockwood Jul 15 '12 at 01:31
  • Unfortunately, I don't know practically anything about loops, because I followed a tutorial that gave me all the code I needed, but not the knowledge to enhance beyond what the tutorial already gave me. – epicness42 Jul 15 '12 at 07:28
  • The problem is that I cannot access logotimer from my onClick method because it is isolated within the Thread. Am I making any sense? – epicness42 Jul 15 '12 at 07:36
  • I have tried moving the thread to include the onClick method and moving the onClick method to extend the thread. – epicness42 Jul 15 '12 at 07:39

4 Answers4

3

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) {
...
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • I'm afraid that when I tried to apply this answer in my code, it failed, however if you could put it in context of the original code I might be able to accurately duplicate it. Would that be possible? Sorry for the inconvenience. – epicness42 Jul 16 '12 at 03:46
  • Well, the first splash screen displays normally, then, it skips the second splash screen, goes to the menu, then it goes back to the second splash screen and finally, it reloads the menu. – epicness42 Jul 16 '12 at 04:41
  • I have discovered why and it is not due to your solution. :) – epicness42 Jul 16 '12 at 17:59
  • Unfortunately, it still does not function correctly. What goes here, protected void onCancel("...")? – epicness42 Jul 16 '12 at 18:05
3

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)

wnafee
  • 2,126
  • 16
  • 23
  • I could very well be wrong, however I do not think it is possible to reference "logotimer" from outside the thread. I am new to java and appreciate your input. – epicness42 Jul 15 '12 at 05:36
  • of course it's possible. just define `Thead logotimer;` as a global variable in the class. Then in your `onCreate()` method you instantiate a new thread object as you did above by using `logotimer = new Thread() {....}`. Now you will have access to it in your `onClick()` method to call `logotimer.interrupt()`. You should see your `InterruptedException` get triggered in your thread – wnafee Jul 15 '12 at 09:40
  • I understood most of that, and experimented with it a little, however it failed because I could not entirely grasp the concept. I was wondering if you could use some example code to illustrate what you are saying. – epicness42 Jul 15 '12 at 20:07
  • I am going to post the code derived as a result of your answer in a new answer – epicness42 Jul 15 '12 at 22:36
  • What you posted looks fine, but i dont see where you tie your onClick listener to the view that you're trying to click on. Based on your description, it seems that you have a splash screen (i presume an `ImageView`). If this is the case, then you should tie the onClick listener to your `ImageView`. Try that out and let me know what you find. – wnafee Jul 17 '12 at 14:00
  • I did things a little differently; piecing together online tutorials, but if I am not mistaken, I referenced my ImageView in the speed2 thingy. "speed2" refers to the onClick value in my xml. – epicness42 Jul 17 '12 at 16:31
  • Is there a better way to do it? – epicness42 Jul 17 '12 at 16:34
  • Does this create a new "logotimer" that does nothing and then interrupt it when the screen is pressed? – epicness42 Jul 18 '12 at 00:23
  • I have found the answer. I created a new, global, logotimer different than the one used in the loop that was interrupted in my onClick method instead of interrupting the original thread and thus breaking the loop. All I had to do was remove the second logotimer declaration and it worked. Thank you so much for all of your help! – epicness42 Jul 19 '12 at 22:10
0
boolean exit = false;
int logotimer = 0;
while(logotimer <5000 && exit != false) {
  sleep(100);
  logotimer = logotimer +100;
  // value = ???    
  if(logotimer == value) {
      exit = true;
  }
}
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
Aaron
  • 11,239
  • 18
  • 58
  • 73
0
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"));
}






}
epicness42
  • 1,137
  • 11
  • 12
  • this looks fine as long as `speed2()` is truly your `onClick` for your imageview. Put a breakpoint at your catch clause in the thread code to verify that this is actually being triggered when you do an interrupt. – wnafee Jul 18 '12 at 09:56
  • Your mistake is that you created a new local variable `logotimer` inside your `onCreate()` instead of using your global variable. In your `onCreate()` change `Thread logotimer = {...}` to `logotimer = {...}` – wnafee Jul 21 '12 at 12:50
  • Or you can just remove the second logotimer declaration. – epicness42 Jul 21 '12 at 18:18