thanks you for help i cleared this using volley liabraries.
I have a app in which there are 12 AsyncTask
, which are used to downloa data from server. I want to navigate to next activity on button click. When user click on button i am stopping or cancelling all AsyncTask
, but this process leads to lagging or hang screen for 5 to 10 second before proceeding to next activity.
According to me, this is happening,because it is cancelling AsyncTask
.Is there any better way to switch activity? i am swiching successfuly but hanging for some time.
Asked
Active
Viewed 74 times
0

android_jain
- 788
- 8
- 19
-
Cancelling an `AsyncTask` should be very fast. Do you have custom logic in their `onCancelled` functions that would be taking a long time to process? – degs Jan 27 '17 at 05:50
-
if (g1 != null){ g1.cancel(true);} if (g2 != null){ g2.cancel(true);} if (g3 != null){ g3.cancel(true);} if (g4 != null){ g4.cancel(true);} if (g5 != null){ g5.cancel(true);} if (g6 != null){ g6.cancel(true);} if (g7 != null){ g7.cancel(true);} – android_jain Jan 27 '17 at 05:52
-
g1 is asyncktask object an i have g1 to g12 – android_jain Jan 27 '17 at 05:52
-
Possible duplicate of [Android - Cancel AsyncTask Forcefully](http://stackoverflow.com/questions/4748964/android-cancel-asynctask-forcefully) – Vivek Mishra Jan 27 '17 at 05:55
-
its not duplicate i have issue about hanging to swiching activity whith multiple Asyncktast – android_jain Jan 27 '17 at 05:58
-
Do you know for certain that cancelling `AsyncTask` is the culprit? Have you tried profiling to see which method is eating up the time? – degs Jan 27 '17 at 06:02
-
i dont khow can you explain is proffiling – android_jain Jan 27 '17 at 06:09
-
volley i can cancel easily – android_jain Apr 01 '17 at 06:02
-
thanks all for helping – android_jain Apr 01 '17 at 06:02
2 Answers
1
Please try writing the code in a separate ui thread as follows:
new Handler().post(new Runnable() {
@Override
public void run() {
// start your new activity here
}
});

Shahid Sarwar
- 1,209
- 14
- 29
-
-
@sourabhjain if this solution works for u please accept the answer. – Shahid Sarwar Jan 27 '17 at 06:08
-
1
Try using background service class, so there won't be any lagging while switching activity.

Kevin Kulothungan
- 46
- 5
-
i m using background task ?? any difference between do in background and background service – android_jain Jan 27 '17 at 06:33
-
background task will be associated with the respective activity class, but background service is independent. Even if you finish the class it won't affect your service. – Kevin Kulothungan Jan 27 '17 at 06:49