Questions tagged [android-asynctask]

Use for questions on android.os.AsyncTask

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most).

WARNING : The AsyncTask has an implicit reference to the enclosing Activity. If a configuration change happens the Activity instance that started the AsyncTask would be destroyed, but not GCd until the AsyncTask finishes. Since Activities are heavy this could lead to memory issues if several AsyncTasks are started. Another issue is that the result of the AsyncTask could be lost, if it's intended to act on the state of the Activity. Replace the AsyncTask by the new AsyncTaskLoader

More information:

14544 questions
60
votes
3 answers

WeakReference/AsyncTask pattern in android

I have a question regarding this simple frequently occurring situation in android . We have a main activity , we invoke an AsyncTask alongwith the reference of the mainactivity , so that that the AsyncTask can update the views on the…
Muhammad Ahmed AbuTalib
  • 4,080
  • 4
  • 36
  • 59
59
votes
5 answers

How to execute Async task repeatedly after fixed time intervals

How to make Async task execute repeatedly after some time interval just like Timer...Actually I am developing an application that will download automatically all the latest unread greeting from the server and for that purpose I have to check for…
Waseem
  • 1,392
  • 5
  • 21
  • 30
59
votes
5 answers

Why to use Handlers while runOnUiThread does the same?

I have come across both Handlers and runOnUiThread concepts. But to me it still seems to be a doubt as on which facts do they differ exactly. They both are intended to do UI actions from a background thread. But what are the factors that are to be…
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
59
votes
12 answers

Android ProgessBar while loading WebView

In my application, I have a WebView which loads any URL from the internet. Now, sometimes due to slow networks the page takes a long time to load and the user sees only a blank screen. I want to show a ProgressBar while the WebView gets loaded and…
Swayam
  • 16,294
  • 14
  • 64
  • 102
55
votes
5 answers

Preventing the back button from cancelling a DialogFragment

I have a Fragment that can create and pop up a DialogFragment, but when I hit the back button, it dismisses the dialog even though I explicitly call setCancelable(false); Is there any way for my DialogFragment to be insensative to the back…
MattF
  • 1,475
  • 3
  • 16
  • 18
54
votes
8 answers

Should I use AsyncTask or IntentService for my application?

I have been reading around on internet connectivity with Android and noticed there are different ways to handle this i.e. AsyncTask and IntentService. However, I'm still not sure which one to use. My application is basically a location/trails finder…
Johnathan Au
  • 5,244
  • 18
  • 70
  • 128
54
votes
8 answers

How to get a string back from AsyncTask?

I have the following class: public class getURLData extends AsyncTask{ @Override protected String doInBackground(String... params) { String line; try { DefaultHttpClient httpClient = new…
Paul
  • 2,465
  • 8
  • 35
  • 60
53
votes
3 answers

Showing status for the last list item in a ListView using ViewHolder

int globalPosition ; .............. buttonAllData.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new…
Sun
  • 6,768
  • 25
  • 76
  • 131
53
votes
4 answers

android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)

I am developing my first Android app and had built in mostly based on research. When I try to login my user I get the android.os.NetworkOnMainThreadException error. I have read online that Async should be used to make sure that this error doesn't…
Jason6916
  • 631
  • 1
  • 5
  • 8
52
votes
5 answers

android design considerations: AsyncTask vs Service (IntentService?)

I'm designing an android app which will need to do the following steps: user pushes a button or otherwise indicates to "sync data". sync process will use REST web services to move data to and from the server. the data will be stored locally in a…
tia
  • 1,004
  • 3
  • 16
  • 21
52
votes
6 answers

CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch views

I have an issue with the following error in Android: CalledFromWrongThreadException;: Only the original thread that created a view hierarchy can touch its views It appears to happen when I try to update a Textview in my Activity, the call to…
Donal Rafferty
  • 19,707
  • 39
  • 114
  • 191
51
votes
11 answers

AsyncTask in Android with Kotlin

How to make an API call in Android with Kotlin? I have heard of Anko . But I want to use methods provided by Kotlin like in Android we have Asynctask for background operations.
Rakesh Gujari
  • 899
  • 1
  • 8
  • 12
50
votes
9 answers

How to run an async task for every x mins in android?

how to run the async task at specific time? (I want to run it every 2 mins) I tried using post delayed but it's not working? tvData.postDelayed(new Runnable(){ @Override public void run() { readWebpage(); }}, 100); In the…
Shan
  • 2,822
  • 9
  • 40
  • 61
48
votes
6 answers

Using wait in AsyncTask

When using a wait in an AsyncTask, I get ERROR/AndroidRuntime(24230): Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before wait() Is it possible to use an Asynctask just for waiting? How? Thanks class WaitSplash…
jul
  • 36,404
  • 64
  • 191
  • 318
47
votes
4 answers

Is using event library like Otto or EventBus a recommended way to handle relations between Activities, Fragments, and background threads

In most of the case, when dealing with case User thread (AsyncTask) to perform background processing Pass back calculated result back to Activity or Fragment Activity or Fragment re-creation might happen before user thread finishes its background…
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875