Presentation is loading. Please wait.

Presentation is loading. Please wait.

Android Topics Asynchronous Callsbacks

Similar presentations


Presentation on theme: "Android Topics Asynchronous Callsbacks"— Presentation transcript:

1 Android Topics Asynchronous Callsbacks
Synchronous vs Asynchronous Overview AsyncTask Explore AsyncTask documentation Basic Understanding of AsyncTask callbacks AsyncTask parameters Explore a Sample Structure of a simple AsyncTask Class Another Examlpe Rules for using AsyncTask Practice Review

2 Topic 1: Synchronous vs. Asynchronous
Most of the code that you've written has been synchronous. Synchronous means that each line of code gets executed one right after another. Synchronous code requires us to wait for each line to finish executing before going on to the next line. Asynchronous uses callbacks to remove this sequence.

3 Quick look at Synchronous vs Asynchronous
Waiting for something to happen (and doing nothing else until it happens!) before proceeding. Asynchronous Do something else while you’re waiting for the event to happen. When the event happens, you will get called back, and you can jump into action.

4 Basics of Async Callbacks in Android
Asynchronous means that we tell the system we're interested in a certain event, like a click event. The system will notify us, or call us back when that event happens. In the meantime, we can go do something else in the app. This means we're not sitting around stalled or waiting. We can still do other things like scroll a list, or wait for other user input events. When the event happens, or if it even happens at all, the system will call us back and we can execute instructions that we specified.

5 Asynchronous callbacks
is a common pattern in mobile apps.

6 Topic 2: OverView of AsyncTask
Use an Android AsyncTask to execute some long-running code that needs to update the UI. Useful for the following: Run a specified long-running task on another thread Once this thread completes the specified task, it will perform updates to the UI. An asynchronous task (AsyncTask) is defined by a computation that runs on a background thread and whose result is published on the UI thread.

7 Topic 3: Explore AsyncTask documentation
Explore how to set up listeners for events. Learn how to hook into these events and be able to respond to them. What did you discover?

8 Basic Understanding of AsyncTask callbacks
An asynchronous task is defined by 4 steps: onPreExecute, doInBackground, onProgressUpdate and onPostExecute.

9 onPreExecute() Invoked on the UI thread before the task is executed.
This step is normally used to setup the task.

10 doInBackground(Params...)
invoked to perform your long-running work. The input parameters from execute are passed into this method invoked on the background thread immediately after onPreExecute() has finished executing.

11 onProgressUpdate(Progress...)
invoked on the UI thread after a call to publishProgress(Progress...). This method is used to display any form of progress in the user interface while the background computation is still executing

12 onPostExecute(Result)
This method is used to update the UI once the background work is completed. This callback is invoked on the UI thread. The result of the background computation is passed to this step as a parameter.

13 AsyncTask Parameters AsyncTask utilizes three parameters:
android.os.AsyncTask<Params, Progress, Result> These parameters can be of any type.

14 AsyncTask Parameters The three types used by an asynchronous task are the following: Params — the type of the parameters sent to the task upon execution the type of parameters passed into execute(). Progress — the type of the progress units published during the background computation. Result — the type of the result of the background computation must be the return type of doInBackground(). Example: private class MyAsyncTask extends AsyncTask<String, Void, int > Params Progress Result

15 AsyncTask Parameters Not all parameters need to used by an asynchronous task. To mark a type as unused, use the type Void: Example: private class MyTask extends AsyncTask<Void, Void, Void> { ... }

16 Explore the Structure of a simple AsyncTask Class

17 private class MyAsyncTask extends AsyncTask<X, Y, Z>
protected void onPreExecute(){ This method is executed before starting the new Thread. Use onPreExecute to initialize and setup. NOTE:The return type is void and there are no parameters. } protected Z doInBackground(X...x){ Place all the background workload in this method. doInBackground() uses a parameter of type X (array of objects from the type “X”). doInBackground() returns an object of type “Z”. protected void onProgressUpdate(Y y){ onProgressUpdate is called using the method publishProgress(y). This method is called when you want to update UI components in the Activity layout. onProgressUpdate () uses a parameter of type Y. protected void onPostExecute(Z z){ onPostExecute is called after doInBackground() has completed its work. NOTE: doInBackground() sends a value of type Z to onPostExecute().

18 Further Explanation of Parameters Refer to the previous slide.
X : The type of the input variables value you want to set to the background process. This can be an array of objects. Y : The type of the objects you are going to enter in the onProgressUpdate method. Z : The type of the result from the operations you have done in the background process.

19 Another Example

20 Instantiating and executing an AsyncTask
An AsyncTask object must be instantiated and called with the execute() method. Example: MyAsyncTask myTask = new MyAsyncTask (); myTask.execute(x); //x is an input parameter of the type X.

21 new HeavyDutyTask().execute();
private class HeavyDutyTask extends AsyncTask <double, Void, int> { @Override protected void onPreExecute() { // Perform setup - runs on user interface thread } protected int doInBackground(double…d) { // Insert heavy duty task work here! Runs in the background. protected void onProgressUpdate(Void… param) { // Update user with progress information or similar - runs on UI Thread protected void onPostExecute(int result) { // Update user interface. Runs on the UI Thread To execute this code: new HeavyDutyTask().execute();

22 Rules for using AsyncTask
Do not call the methods onPreExecute, doInBackground and onPostExecute manually. These are callback methods and will be performed automatically done by the system.  You cannot call an AsyncTask inside another AsyncTask or Thread. The call of the method execute must be done in the UI Thread. The method onPostExecute is executed in the UI Thread (here you can call another AsyncTask!). The input parameters of the task can be an Object array, this way you can put whatever objects and types you want.

23 Final Review Three ways to perform work in the background:
Native Threads: No Handlers Threads with Handlers (MessageQueue) AsyncTask

24 Native Threads (No Handlers)
Pros: Android supports standard (native) Java Threads. This means that you can use native threads to execute actions in the background. Cons: The only limitation is that you cannot directly update the UI from a background thread. If you need to update the UI from a background thread you need to use a Hander or a AsynTask.

25 Threads with Handlers (MessageQueue)
Pros: Android supports the pairing of standard Java Threads with Handlers. Messages can be passed back to the UI Thread by using a Handler's post method. A Handler allows you to communicate back with the UI thread from a standard background thread. This is useful in android as Handler can send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue. When a new Handler is created, it is bound to the thread/message queue of the thread that is creating it. This method is useful for long-running heav-yduty tasks. Cons: Requires more code and is not always useful for very short tasks that do not impact the UI.

26 AsyncTask Pros: The AsyncTask class encapsulates the creation of a background process and the synchronization with the UI Thread. AsyncTask is easy to use because it allows programmers to perform background operations and publish results on the UI thread without having to manipulate threads and handlers. Cons: By default, an app pushes all of the AsyncTask objects it creates into a single thread. No always useful for very long tasks.


Download ppt "Android Topics Asynchronous Callsbacks"

Similar presentations


Ads by Google