We have to use Asynchronous Task to start our new Activity on Tab Click event but in the ListView or any view we can directly can start the new activity Why?
http://developer.android.com/reference/android/os/AsyncTask.html
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.
An asynchronous task is defined by a
computation that runs on a background
thread and whose result is published
on the UI thread.on the UI thread.
Basically you want to avoid that the loading process/device hangs when loading loads of data to the list initially, that's why you make it async, outside the UI thread, so the user can use the app while data is loading in the background.
Starting an activity is faster than loading lots of initial data into a long list view, especially if it's remote data from a remote server. Therefore the app you're looking at is probably using this here.
If you want to perform some task in background at the same time you want to do another task at the forground.
http://developer.android.com/reference/android/os/AsyncTask.html
This link surely will help you.
You just try this one will help you.
http://xoriant.com/blog/mobile-application-development/android-async-task.html
Related
I finished reading the Cursors and AsyncTasks chapter in Head First Android Development, and I'm still not sure when to use AsyncTask and when not to when performing database operations.
Suppose I'm at an activity which, when it ends, writes to sqlite database and sends the user back to the main activity (using finish()), which displays a list of data that is fetched from the database.
The list of data obviously has to update instantly, just as the user enters back to MainActivity. Does this mean I shouldn't use AsyncTask in this situation? What other choices do I have here? I don't want to let the user see the changed list of data after MainActivity is already visible.
A simpler scenario: The user clicks a button that should display database information in a TextView. Should this be done in a separate thread? I'm not sure, because the TextView must be updated immediately.
The best practice for read & write data in the database is: do it in a separate thread. Because it can take a lot of time, if the amount of data is fairly large or the query itself is too complicated, do it in the main thread can cause ANR.
If you want to display data changes immediately, you can do it as following:
place a method in the ui activity to change ui according to the parameters
triggered update ui through invoke the method above in the async callback
as for your last example, u can update the content of the textview immediately in the onPostExecute method of AsyncTask class
if you are not using AsyncTask u can just update ui in other thread through one of the ways below:
Activity$runOnUiThread(Runnable runnable)
new Handler(Looper.MainLooper()).post(Runnable runnable) //create handler for main thread, and post runnable to execute in main thread
send message to message queue, then handle the message and update ui in the main thread's handler
Normally you should do database queries on a separate thread, but if you're sure the database is relatively small(light) you could perform it on the main thread. but its always best practice to do potential long running tasks on a different thread.
what is meant by asynchronously loading data in activity or fragment in android?
This is my question. I searched everywhere. I'm not getting a generalized definition for this?. I can't get the term mentioned in android developer also.
Can anyone provide me the basic explanation of this term?
Asynchronous in Android mean that you do stuff while the user can interact with the User Interface (UI) : you are not blocking the UI while you are doing long stuff. So the user can still navigate, change activities or fragment and your data is still loading.
For data : you load it, parse it and do whatever you want in a NON-UI Thread (using AsyncTask eg) and then notify the UI, and display what you need to.
You have many possibilities to implement Asynchronous load in Android, and you have many different way to manage your request. I personnaly recommend using Retrofit if you need to use a Web API.
It means that you load your data in a separate thread than the UI thread. You launch your HTTP request for example in another thread and when it finished you notify the UI thread to refresh display.
This mean to load data in separate thread rather than load the data in main thread.Loading data in main thread may cause app to block
The AsyncTask class encapsulates the creation of a background process and the synchronization with the main thread. It also supports reporting progress of the running tasks.
To use AsyncTask you must subclass it. AsyncTask uses generics and varargs. The parameters are the following AsyncTask .
An AsyncTask is started via the execute() method.
The execute() method calls the doInBackground() and the onPostExecute() method.
TypeOfVarArgParams is passed into the doInBackground() method as input, ProgressValue is used for progress information and ResultValue must be returned from doInBackground() method and is passed to onPostExecute() as a parameter.
The doInBackground() method contains the coding instruction which should be performed in a background thread. This method runs automatically in a separate Thread.
The onPostExecute() method synchronizes itself again with the user interface thread and allows it to be updated. This method is called by the framework once the doInBackground() method finishes.
I have a activity that show one listview. In the activity I have a one AsyncTask (named here of AsListView) to get values from internet and fill some informations in each item of the listview. Work fine.
Now I created a button in ActionBar to show one image from streetview. To do this I have implemented another AsyncTask (named here of AsImage) to get image from google and show in a DialogFragment, but is necessary wait the execution of all AsListView Threads. It spends long time depending of the number of items in the list.
To execute AsImage rapidily, I cancel all AsListView tasks, but it's not good for me (user loss informations). The ideal soluction is set AsListView tasks to wait while AsImage execute. When AsImage finish I set AsListView tasks to continue execution. But I know that is not possible handle the control of execution of AsynkTasks.....
Some solution?
You can try to synchronize the AsyncTasks using a CountDownLatch.
Otherwise if you want you can use Threads instead of AsyncTasks and set their priorities, but there is a reason android made the AsyncTask class so I recommend you use it.
I have a a service class which includes an Async task. In the doInBackground and onPostExecute I call some methods which are in the service class but outside the Async task. When these methods get called will they still be in the thread created by the Async task and therefore not interfering with the main UI.
To illustrate my lack of understanding a bit more should I try to get almost everything that the service class does into the Async task. For example the service starts up as the result of an alarm and in the onStartCommand sets a repeating alarm (this is as Reto Meire's Earthquake example)and creates a database. Would it make sense to move the code for these two operations into the onPreExecute part of the Async task?
No need to do that.. make sure that the method which you want to run in background is called from doInBavkground().. rest you can put in postExecute.... the snippet which you want to run after the backGround task should be in PostExecute(). If You call methods from doInBackground() they still run on background thread.. does't matter where they are.. and if you call it from onPostExecute() then it will run on other thread which ofcourse can make changes in your display.. just like showing dialog etc...
You should always offload as much work as possible to background threads/tasks. Something like database creation should be done in the doInBackground method. The onPreExecute and onPostExecute methods run on the UI thread and are generally used to inform the user of background activity (e.g. using a ProgressDialog).
From experience, I also suggest not using a background service if you can get away with it. If you use one, you should know how to clean it up properly since users generally get annoyed with an application running in the background.
I'm currently using asyncTask() to do some background exchanging of bitmap images as my activity progresses, and all works just fine; until I end the activity where the task resides. The task's thread goes into "wait" status instead of being destroyed? I've cancelled, and checked the return value of .isCancelled() as well. This wouldn't really be a problem except when I restart my activity again from a MAIN activity it will actually make a new thread for the new asyncTask(); thus leaving the old one sitting there "waiting" in the background? Is this a bug, or am I simply using this feature incorrectly?
AsyncTask uses a thread pool. It is normal for you to see 4/5 async tasks in your debug panel. Just make sure that your async tasks do not hold strong references to the activity (try to make those async tasks static inner classes (or event separate classes) and have them hold a WeakReference to the activity instead of a strong reference.
Background task, progress dialog, orientation change - is there any 100% working solution?
AsyncTask threads never die
Simple Thread Management - Java - Android
etc. > Try to search "android asynctask thread pool" to learn more.
i think you should use static flag variable in your doInBackground function for terminating operation or loop. In that way you can achieve your task