My code runs fine, but if I go back to the home screen and return the code stops running on the text view shows nothing. Shouldn't it restart when I return to the page? It throws no errors, but if I eliminate the task and initialize the app it all over again it works but I must stay on that activity because If I go to Home and return it stops.
public class IsstatusActivity extends AppCompatActivity {
JSONParser jsonparser = new JSONParser();
TextView latitude;
TextView longitude;
String lat;
String longit;
JSONObject jobj = null;
private final ReentrantLock lock = new ReentrantLock();
private final Condition tryAgain = lock.newCondition();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.isstatus);
latitude = (TextView) findViewById(R.id.Coordinates);
longitude = (TextView) findViewById(R.id.longitude);
new Retrievedata().execute();
}
class Retrievedata extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
while (!isCancelled()) {
jobj = jsonparser.makeHttpRequest("http://api.wheretheiss.at/v1/satellites/25544");
String lat = "latitude : " + jobj.getString("latitude");
String longit ="longitude : " + jobj.getString("longitude");
// this will cause onProgressUpdate to be called with lat & long
publishProgress(lat,longit);
// it's okay to sleep within the background thread
Thread.sleep(1500);
}
} catch (InterruptedException e) {
Log.w("RetrieveData", "thread was interrupted", e);
} catch (JSONException e) {
Log.e("RetrieveData", "parse error", e);
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
latitude.setText(values[0]);
longitude.setText(values[1]);
}
}
}