Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Timer Stop

mHandler = new Handler();
    Runnable runnable = new Runnable(){

        @Override
        public void run(){
            while(Running){
                try{
                    Thread.sleep(1000);}
                        catch(InterruptedException e){
                            e.printStackTrace();
                        }
                            mHandler.post(new Runnable(){
                                @Override
                                public void run(){
                                    mtimerinbackground--;
                                    mTimer.setText(String.valueOf(mtimerinbackground));

                                    if(mtimerinbackground == 50){
                                        mtimerinbackground = 60;
                                        mTimer.setText(String.valueOf(mtimerinbackground));
                                        Intent intent = new Intent(PlayScreen.this, Statistics.class);
                                        startActivity(intent);
                                    }
                                }
                            });
                    }
                }
        };
        new Thread(runnable).start();

Okay so this is a timer and I don't know how to stop it. Could someone please help me??

1 Answer

Well I can't really make out much from this code snippet. What I can guess as of now that there could be a problem of a race condition where more than one instance of a thread is calling the same Intent object. It's just a guess as of now. If you could share some of the main context of the code here maybe it would get more clearer.