In case of a big problem with AsynchTask, when you finish your activity, AsynchTask loses its track with your user interface. After that, when you return to this activity, the progressBar is not updated, even if the download progress is still running in the background. In fact, AsynchTask does not belong to the new activity that you started, so the new instance of the progress bar in the new Activity will not be updated. To fix this problem, I suggest you:
1- Run the thread with the Task timer in onResume (), which updates the ur progress bar with values updated from the AsyncTask background. Something like that:
private void updateProgressBar(){ Runnable runnable = new updateProgress(); background = new Thread(runnable); background.start(); } public class updateProgress implements Runnable { public void run() { while(Thread.currentThread()==background) try { Thread.sleep(1000); Message msg = new Message(); progress = getProgressPercentage(); handler.sendMessage(msg); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (Exception e) { } } } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { progress.setProgress(msg.what); } };
and when your activity is not visible, you must destroy the thread:
private void destroyRunningThreads() { if(background!=null) { background.interrupt(); background=null; } }
2- Define a global static boolean variable. Set true to onPreExecute and set to false onPostExecute. It shows what you are loading or not, so you can check if this variable is true, display the previous dialog of the progress bar. (You can do something like this with an integer or an array of integers to show the update percentage for each download process).
3- The last method that I personally used was to show the download process in the notification panel, and in my list view, just show that it is loading right now or not. (using the 2nd method with boolean values). Thus, even if you finish the work, the notification panel is still updated with the progress of the download.
source share