Stop a thread in the onPause() method that was created inside the onCreate() method
<p>So I have it already that my thread will stop when the "Back" Button is pressed on screen. However, I want to make it possible so that the thread stops when the user either presses the home or the back key. </p>

<p>What I have so far is: 
now what I want is to implement all the stopping methods necessary, only this time in the onPause() method as well as the back button. As you can see, thread is currently out of context when trying to call it in the onPause() method since I created it in the onCreate() method. I know I am probably over thinking this, as I already attempted to create separate classes to extend and implement Thread and Runnable respectively. Any help on how I might achieve this is very appreciated. </p>

<pre><code>public class TimerPage extends Activity{
    public boolean stopThread = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timerpage);

        final EditText et = (EditText)findViewById(R.id.fullview);
        final Button b = (Button)findViewById(R.id.back);
        final DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        final Handler handler = new Handler(); //Handler for the thread


        final Runnable r = new Runnable() { //Declare the runnable, used in the thread
            public void run() 
            {
                Date date = new Date(); 
                String currentTime = "The time is: " + dateFormat.format(date);
                et.setText(currentTime);


                Context context = getApplicationContext();
                CharSequence text = "Still stuck in the thread...";
                int duration = Toast.LENGTH_SHORT;
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();


                handler.postDelayed(this, 5000); //Handles and delays the thread for 5 seconds 
                handler.removeCallbacks(this);
            }
        };

        final Thread thread = new Thread() //Declare the thread
        {
            @Override
            public void run() { //On run...
                try {
                    while(stopThread) {
                        sleep(5000); //Sleep for 5 seconds
                        handler.post(r); //Complete the handler
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        thread.start(); //Start the thread

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                thread.interrupt(); //Basically pause the thread from advancing
                thread.stop(); //Stop the thread from running again
                stopThread = false; //Fail-safe to stop the thread

                Intent i = new Intent(TimerPage.this, HelloRelativeLayoutActivity.class); //Go back to HelloRelativeLayout activity
                startActivity(i);
            }
        });
    }


    @Override
    public void onPause() {
        thread.interrupt(); //Basically pause the thread from advancing
        thread.stop(); //Stop the thread from running again
        stopThread = false; //Fail-safe to stop the thread

        Intent i = new Intent(TimerPage.this, HelloRelativeLayoutActivity.class); //Go back to HelloRelativeLayout activity
        startActivity(i);
    }

}
</code></pre>
