title: Runnable, Pause and Resume thread
body: <p>my app is working fine but i cant figure out why my void run(); still runs in the background when i click the "home" button.</p>
  
  <p>I want my thread to pause the Thread onPause and start the thread again when onResume() i called!</p>
  
  <p>Cant figure out what im doing wrong!</p>
  
  <p>My code:</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
  Thread thread;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  
      thread = new Thread(new MyThread());
      thread.start();
  
  }
  
  
  @Override
  protected void onPause() {
      super.onPause();
      this.thread.interrupt();
  
      Log.d("Method:","OnPause();");
  
  
  
  }
  
  @Override
  protected void onResume(){
      super.onResume();
      this.thread.run();
      Log.d("Method:","onResume();");
  
  }
  
  
  @Override
  protected void onDestroy(){
      super.onDestroy();
      this.thread.interrupt();
      finish();
      Log.d("Method:","onDestroy();");
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Inner class:</p>
  
  <pre><code>public class MyThread implements Runnable{
  
      @Override
      public void run() {
  
          while(!Thread.currentThread().isInterrupted()){
              if(Thread.currentThread().isInterrupted()){
  
                  break;
              }
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              Log.d("Method","void run()");
              //Creates a file with SharedPreferences and make editable
              SharedPreferences preferences = getSharedPreferences("MyPrime", Context.MODE_PRIVATE);
              final SharedPreferences.Editor editor = preferences.edit();
  
              Random random = new Random();
              BigInteger bigInteger = new BigInteger(10,random);
  
              //Loads and gets the last int value store in the file
              SharedPreferences preferences1 = getSharedPreferences("MyPrime",Context.MODE_PRIVATE);
              final int numberInFile = preferences1.getInt("MyPrime",0);
  
              if(bigInteger.isProbablePrime(random.nextInt(500))){
                  if(numberInFile &lt; bigInteger.intValue()){
                      editor.putInt("MyPrime",bigInteger.intValue());
                      editor.commit();
  
                  }
              }
          }
  
      }
  
  }
  </code></pre>