title: How to stop tracking after coming back to the same activity
body: <p>I have a tracking application which is showing longitude and latitude.</p>
  
  <p>I have a screen which have 3 buttons showlocation , start tracking and stop tracking. Now what i want is that the tracking will remain continue until i click the stop button. Now thing is when i stays on the same activity, the start tracking and stop tracking is working fine but when i only click the start tracking button and go back to other activities and then come back to that particular activity and clicking the stop button the activity is crashing and if i click start tracking button the tracking starts again which means the timer which i executed is working 2 times.</p>
  
  <p>So i want 2 solutions:
  1. When the one timer is working on the click of button to other timer will not start on again clicking the start button.
  2. And when i come back to the activity and click the stop button the timer should stop.</p>
  
  <p>Here is my code</p>
  
  <pre><code>public class GPSTrackingActivity extends Activity {
  
  Timer timer;
  TimerTask timerTask;   
  final Handler handler = new Handler();
  
  // GPSTracker class
  GPStracker gps;
  double longitude, latitude;
  Button showLocation1,timerstopped,starttimer;
  
  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_gpstracking);
   showLocation1 = (Button) findViewById(R.id.showlocation1);
  
   // show location button click event
   showLocation1.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View arg0) {        
              // create class object
              // create class object
              gps = new GPStracker(GPSTrackingActivity.this);
  
              // check if GPS enabled     
              if(gps.canGetLocation()){
  
                  double latitude = gps.getLatitude();
                  double longitude = gps.getLongitude();
  
                  // \n is for new line
                  Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();    
              }else{
                  // can't get location
                  // GPS or Network is not enabled
                  // Ask user to enable GPS/network in settings
                  gps.showSettingsAlert();
              }
          }
      });
  
   starttimer =(Button) findViewById(R.id.starttimer);
  
   // stop showing location button click event
   starttimer.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View arg0) {                
  
              // create class object
              startTimer();
               Toast.makeText(getApplicationContext(), "Your tracking is now started " , Toast.LENGTH_SHORT).show();          
  
              }
      });
  
  
  
   timerstopped =(Button) findViewById(R.id.timerstopped);
  
   // stop showing location button click event
   timerstopped.setOnClickListener(new View.OnClickListener() {
  
  
  
          @Override
          public void onClick(View arg0) {
              timer.cancel();
              timerTask.cancel(); 
               Toast.makeText(getApplicationContext(), "Your tracking is now stopped " , Toast.LENGTH_SHORT).show();          
  
              }
      });
  
  }
  
  
  
  
  public void startTimer() {
  
              //set a new Timer       
            timer = new Timer();
  
            //initialize the TimerTask's job      
            gpsmethod();      
  
              //schedule the timer, after the first 1000ms the TimerTask will run every min
  
              timer.schedule(timerTask, 1000, 10000); //
  
          }
  
  
  public void gpsmethod() {
      timerTask=new TimerTask() {
  
          @Override
          public void run() {
              // TODO Auto-generated method stub
              handler.post(new Runnable() {
  
                  @Override
                  public void run() {
                      // TODO Auto-generated method stub
                      GPStracker gps = new GPStracker(GPSTrackingActivity.this);
                      //call your function for getting lat n lon
                       double latitude = gps.getLatitude();
                     double longitude = gps.getLongitude();
  
                     Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_SHORT).show();
                  }
              });
          }
      }; 
  }
  }
  </code></pre>