0

I have a thread created inside a secondary activity on Android, like so:

new Thread(new Runnable() {
                public void run() {
                    try {
                        String branch=spinner.getSelectedItem().toString();

                        while( branch.equals(spinner.getSelectedItem().toString())){
                            System.out.println("----LOOPER."+spinner.getSelectedItem().toString());
                            GetQinfo a= (GetQinfo) new GetQinfo().execute(city,type,org,spinner.getSelectedItem().toString());
                            Thread.sleep(refreshRate);                              
                        }
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    } 
                 return;
                }
              }).start();

the problem is that when i go back to the main activity this thread is still running. what i did was on the goback button to write this:

spinner.setSelection(0);
this.finish();

This way the value of the spinner is changed, causing the while loop on the thread to return false, thus exiting the thread. But i dont think this is the right way of doing it. can anyone suggest something different, or should i say, better

Skaros Ilias
  • 1,008
  • 12
  • 40

3 Answers3

1

You should use interrupt() method of the thread when you leave the activity.

YourThread.interrupt();

new Thread(new Runnable() {
                public void run() {
                    try {
if(!Thread.interrupted())
                        String branch=spinner.getSelectedItem().toString();

                        while( branch.equals(spinner.getSelectedItem().toString())){
                            System.out.println("----LOOPER."+spinner.getSelectedItem().toString());
                            GetQinfo a= (GetQinfo) new GetQinfo().execute(city,type,org,spinner.getSelectedItem().toString());
                            Thread.sleep(refreshRate);                              
                        }
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    } 
                 return;
                }
              }).start();
Suhail Mehta
  • 5,514
  • 2
  • 23
  • 37
0

There is no method explicitly available to stop a thread. There were methods previously to stop the thread but they were deprecated. Please read the following link to see why the developers of java wanted it this way: http://docs.oracle.com/javase/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html

According to this document, they prefer that you do it in the manner that you suggested. So you're not wrong.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
0
 private volatile boolean stopThread;

   public void run() {
      String branch=spinner.getSelectedItem().toString();
      while (!stopThread) {
        System.out.println("----LOOPER."+spinner.getSelectedItem().toString());
        GetQinfo a= (GetQinfo) new GetQinfo().execute(city,type,org,spinner.getSelectedItem().toString());
        Thread.sleep(refreshRate); 
      }
   }


public void stopThread() {
    stopThread = true;
    thread.interrupt();
}
Wagner Michael
  • 2,172
  • 1
  • 15
  • 29