I have an activity in which i am running a thread to hit the web service.i want to finish the activity only when the thread is finished.
To make your life easier use Android AsyncTask Object. This provides the same background process as a Thread but handles everything for you. This includes callbacks at different stages of the AsyncTask. This includes once it has finished doing what you ask of it in the background via the onPostExecute() function.
From the documentation:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
You can use Thread.join(), which works for all Java programs (not just Android).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With