title: How do i exit runnable and switch another activity
body: <p>I have a textview timer, i want it stops after 10 seconds of showing on display and after 10 seconds that my current activity jumps to an other activity</p>
  
  <pre><code>Runnable runnable = new Runnable()
      {
          @Override
          public void run()
          {
              while(Running)
              {
                  try{
                  Thread.sleep(1000);
              }catch(InterruptedException e)
              {
                  e.printStackTrace();
              }
                  handler.post(new Runnable()
                  {
                      @Override
                      public void run()
                      {
                          number+=1;
                          tvTimer.setText(String.valueOf(number)); //With setting value of number on textfield ITS POSSIBLE TO SEE THE TIMER
                          if(number==10)
                          {
                              Running = false;
                              handler.removeCallbacks(this);//that does not works for me to stop 
  
                              Intent i = new Intent(getApplicationContext(), SonucMenu.class); 
                              startActivity(i); //Starting my second class
  
                          }
                      }
  
                  });
  
              }
  
          }
  </code></pre>
  
  <p>My Second activity starts but the layout from second activity comes more than one time, what i want is that it comes only one time, and i think this problem depends on that i cant stop runnable</p>
  
  <p>Here My Complete original Code
  MainActivity.class</p>
  
  <pre><code>package com.marburg.leftright;
  
  import java.util.Random;
  import java.util.concurrent.Executors;
  import java.util.concurrent.Future;
  import java.util.concurrent.ThreadPoolExecutor;
  
  import com.marburg.leftright.R;
  
  import android.support.v7.app.ActionBarActivity;
  import android.app.Activity;
  import android.content.Intent;
  import android.media.MediaPlayer;
  import android.os.Bundle;
  import android.os.Handler;
  import android.view.Menu;  
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  
  public class MainActivity extends Activity {
  
   static Random rnd=new Random();
   static int rndN = rnd.nextInt(2);
   TextView inf,tvSc, tvTimer;
   Button btnL, btnR;
   int ScoreRhtg = 0;
   int ScoreFls  = 0;
   String right = "RIGHT";
   String left = "LEFT";
   private int number;
   private Handler handler;
   private boolean Running = true;
   MediaPlayer mySound;
  
  
  
     public void sendData()
  {
   String rData = Integer.toString(ScoreRhtg); //Score Of Right Answer Data setting
   String wData = Integer.toString(ScoreFls);
   Intent i = new Intent(getApplicationContext(), SonucMenu.class); 
   i.putExtra("DATA", rData);
   i.putExtra("DATA2", wData);
   startActivity(i);
  </code></pre>
  
  <p>}</p>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.lr);
  
      Intent intent = getIntent(); //Get Intent from SonucMenu.class to turn back         MainActivity.java from New Button (This Class)
      inf = (TextView)findViewById(R.id.tvInfo);
      btnL = (Button)findViewById(R.id.btnL);
      btnR = (Button)findViewById(R.id.btnR);
      tvSc = (TextView)findViewById(R.id.tvScore);
  
  
       //Set text left or right according new random number
         if(rndN==1){
              inf.setText(right);
              }
              else
              {
                  inf.setText(left);
              }
  
  
  
           //Set 0 for Left
      btnL.setOnClickListener(new View.OnClickListener()
      {
          @Override
          public void onClick(View v)
          {   //Set 0 for Left
              if(rndN==0)
              {
              ScoreRhtg++;    
              tvSc.setText(ScoreRhtg+" Right");
              rndN=rnd.nextInt(2); //Renew the random number
              }
              else
              {
                  ScoreFls++;
                  tvSc.setText(ScoreFls+" False");
                  rndN=rnd.nextInt(2); //Renew the random number
              }
  
              //Set text left or right according new random number
                if(rndN==1){
                      inf.setText(right);
                      }
                      else
                      {
                          inf.setText(left);
                      }
  
              }
          });
      //Set 1 For Right
      btnR.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View arg0) {
              // TODO Auto-generated method stub
              if(rndN==1)
               {
                   ScoreRhtg++;
                   tvSc.setText(ScoreRhtg+" Right");
                   rndN=rnd.nextInt(2); //Renew the random number
               }
               else
               {   
                   ScoreFls++;
                   tvSc.setText(ScoreFls+" False");
                   rndN=rnd.nextInt(2); //Renew the random number
               }
  
               //Set text left or right according a new random number 
               if(rndN==1){
                  inf.setText(right);
                  }
                  else
                  {
                      inf.setText(left);
                  }
  
          }
  
       });
  
  
  
      //FOR TIMER RESPONSIBLE PART OF CODE
  
      tvTimer = (TextView)findViewById(R.id.tvTimer);
       handler = new Handler();
  
      Runnable runnable = new Runnable()
        {
           @Override
          public void run()
          {
               while(Running)
               {
                   try{
                  Thread.sleep(1000);
               }catch(InterruptedException e)
               {
                   e.printStackTrace();
               }
                  handler.post(new Runnable()
                   {
                      @Override
                       public void run()
                       {
                           number+=1;
                          tvTimer.setText(String.valueOf(number)); //With setting  value   of number on textfield ITS POSSIBLE TO SEE THE TIMER
                           if(number==9)
                           {
                               mySound=MediaPlayer.create(MainActivity.this, R.raw.click);
                              mySound.start();
                              Running = false;
                              handler.removeCallbacks(this);//Doesnt works for me
                               sendData();      //sendData function startActivity(i) included
  
                           }
                       }
  
                   });
  
              }
  
           }
  
  
       };
       new Thread(runnable).start();
  
       //END OF FOR TIMER RESPONSIBLE PART OF CODE
  
       }
  
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_settings) {
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  }
  </code></pre>
  
  <p>And my Second Class SonucMenu.java</p>
  
  <pre><code>package com.example.timerdeneme2;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  
  public class SonucMenu extends MainActivity {
  
  
  public TextView tvCorr, tvWro;
  public Button btnNew, btnCls;
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.sonuc_menu);
      Intent i = getIntent();
      Bundle dataBundle = i.getExtras();
      String dataRString = dataBundle.getString("DATA");
      String dataWString = dataBundle.getString("DATA2");
      TextView tv = (TextView)findViewById(R.id.tvCorr);
      TextView tvW = (TextView)findViewById(R.id.tvWro);
      tv.setText(dataRString);
      tvW.setText(dataWString);
  
      btnNew = (Button)findViewById(R.id.btnNew);
         btnNew.setOnClickListener(new View.OnClickListener()
          {
              @Override
              public void onClick(View v)
              {
  
                  Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
                             Toast.LENGTH_LONG).show();
                  Intent myIntent = new Intent(SonucMenu.this, MainActivity.class);
                  //myIntent.putExtra("key", value); //Optional parameters
                  SonucMenu.this.startActivity(myIntent);
                  }
          });
  
         btnCls=(Button)findViewById(R.id.btnEx);
  
         btnCls.setOnClickListener(new View.OnClickListener() {
  
             @Override
             public void onClick(View arg0) {
             android.os.Process.killProcess(android.os.Process.myPid());
                 System.exit(1);
  
             }
         });
  
  
  
  }
  }
  </code></pre>
  
  <p>And XML Layouts lr.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginLeft="16dp"
  android:layout_marginTop="10dp"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.marburg.leftright.MainActivity" &gt;
  
  &lt;TextView
      android:id="@+id/tvResCorr"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" /&gt;
  
  &lt;TableLayout
      android:id="@+id/tableLayout1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignTop="@+id/tvResCorr"
      android:layout_marginTop="14dp"
      android:layout_toRightOf="@+id/tvResCorr" &gt;
  
      &lt;TableRow
          android:id="@+id/tableRow1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
  
          &lt;Button
              android:id="@+id/btnL"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:text="L"
              android:textSize="70dp" /&gt;
  
          &lt;Button
              android:id="@+id/btnR"
              android:layout_width="100dp"
              android:layout_height="100dp"
              android:layout_marginLeft="50dp"
              android:text="R"
              android:textSize="70dp" /&gt;
  
      &lt;/TableRow&gt;
  
      &lt;TableRow
          android:id="@+id/tableRow2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow
          android:id="@+id/tableRow3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
      &lt;/TableRow&gt;
  
   &lt;/TableLayout&gt;
  
   &lt;TextView
      android:id="@+id/tvInfo"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/tableLayout1"
      android:layout_alignRight="@+id/tableLayout1"
      android:layout_below="@+id/tableLayout1"
      android:layout_marginLeft="10dp"
      android:layout_marginTop="200dp"
      android:gravity="center"
      android:text="Info"
      android:textSize="24dp" /&gt;
  
   &lt;TextView
      android:id="@+id/tvScore"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/tableLayout1"
      android:layout_alignRight="@+id/tableLayout1"
      android:layout_below="@+id/tvInfo"
      android:layout_marginLeft="10dp"
      android:layout_marginTop="20dp"
      android:gravity="center"
      android:text="Score"
      android:textSize="24dp" /&gt;
  
  &lt;TextView
      android:id="@+id/tvTimer"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/tableLayout1"
      android:layout_centerHorizontal="true"
      android:text="Time" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>And sonuc_menu.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.marburg.leftright.MainActivity" &gt;
  
  &lt;TableLayout
      android:id="@+id/tableLayout1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_alignParentTop="true"
      android:layout_marginTop="36dp" &gt;
  
      &lt;TableRow
          android:id="@+id/tableRow1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
  
          &lt;TextView
              android:id="@+id/tvResCorr"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Correctness:" /&gt;
  
          &lt;TextView
              android:id="@+id/tvCorr"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="20sp"
              android:text="X" /&gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow
          android:id="@+id/tableRow2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
  
          &lt;TextView
              android:id="@+id/tvResWro"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Wrongness" /&gt;
  
          &lt;TextView
              android:id="@+id/tvWro"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginLeft="20sp"
              android:text="X" /&gt;
      &lt;/TableRow&gt;
  &lt;/TableLayout&gt;
  
  &lt;TableLayout
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/tableLayout1"
      android:layout_below="@+id/tableLayout1"
      android:layout_marginTop="113dp" &gt;
  
      &lt;TableRow
          android:id="@+id/tableRow3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
  
          &lt;Button
              android:id="@+id/btnNew"
              android:layout_width="300dp"
              android:layout_height="wrap_content"
              android:text="NEW" /&gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow
          android:id="@+id/tableRow4"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
  
          &lt;Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="BEST SCORES" /&gt;
      &lt;/TableRow&gt;
  
      &lt;TableRow
          android:id="@+id/tableRow5"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
      &lt;/TableRow&gt;
  
      &lt;Button
          android:id="@+id/btnEx"
          android:layout_width="300dp"
          android:layout_height="wrap_content"
          android:text="EXIT" /&gt;
  
      &lt;TableRow
          android:id="@+id/tableRow6"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;
      &lt;/TableRow&gt;
  &lt;/TableLayout&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>