Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.