Android Async Task dealing with UI - android

I am new to android development. I would like to accomplish a task described as follows:
A main activty which calls external class(the other class would extend AsyncTask) to parse xml and receive json by requesting to web service and starts a ProgressDialog.
The class performs xml and json parsing in its doInBackground method.
In the onPostExecute method after parsing is complete, dismiss the ProgressDialog that was set in the main activity.
I could do this by passing the ProgressDialog object to the parsing class and dismissing the same object in its onPostExecute method.
I think passing an instance of UI object as argument is not a good approach to program, I hope there must be some other ways to work around.
Please suggest.
Thank you

The easiest way to decouple these is to use an interface:
Define a call-back interface (let's call it WorkDoneListener) with a single method: workDone().
Declare your activity class to implement WorkDoneListener and implement workDone() to dismiss the dialog.
Define the AsyncTask's constructor to accept a WorkDoneListener. Stash the reference in a member field.
In onPostExecute, call the listener's workDone() method.

Ted's answer is what you should do if your AsyncTask is too big and you want to declare it in other file. However, keep in mind that usually you declare the AsyncTask inside your UI class:
public class YourActivity extends Activity{
private class YourAsyncTask extends AsynkTask<etc.>{
}
}
In fact, if you are using you AsyncTask from that activity only (I mean, if you are not using it anywhere else), declaring the AsyncTask as a inner class is a good design practice.

Related

Can not Use Async Class inside an Activity . The Activity already Consist of a Main class extending from Activity

public class Server_Activity extends AsyncTask<Void, String, Void> {
}
An error is shown when I write this class:
ClassServer_Activity must either be declared abstract or implement abstract method doInBackground(params).
What is causing this error ?
Add the method doInBackground(). This is because the AsyncTask is an abstract class, which also contains the abstract method doInBackground(). This means, that any class inheriting from the AsyncTask class must also implement the doInBackground() method.
But your title seems to be another question, sounding like you wrote your Server_Activity class in the same file of another activity. in Eclipse this error would sound like:
The public type Testclass must be defined in its own file
If this is the case: Create a new .java file and write there your Server_Activity class.
And don't forget to implement the doInBackground() method there.
You can see an example of this in the android developer api.
implements doInBackground method in your code. Press ctrl+i, choose doInBackground and then enter to make it easy
based on the documentation you have to implement doInBackground. as doInbackground method is declared as abstract.
see documentations
http://developer.android.com/reference/android/os/AsyncTask.html
protected abstract Result doInBackground (Params... params)
Added in API level 3 Override this method to perform a computation on
a background thread. The specified parameters are the parameters
passed to execute(Params...) by the caller of this task. This method
can call publishProgress(Progress...) to publish updates on the UI
thread.
Parameters params The parameters of the task. Returns A result,
defined by the subclass of this task.
in your case Params... params is Void
↳ android.os.AsyncTask

Android with ASYNC Task

I'm wondering where I should place my AsyncTask in my android project. As of right now I'm implementing an AsyncTask as a private class of my activity its running under. What I am going to do is in each activity that has a network call I will implement its own private class of AsyncTask. I have a few questions though
In The preexecute method it says I can interact with the activity and place a spinner or progress bar. I do this by using My_Activity_Class_Name.this. So my question is does that line of code reference the activity the AsyncTask is called from? If so I believe that will be a static method. How do i actually pass in the instance of the class so I can interact with non static functions?
I want to place all my Async code into one class for its respective needs. My quesiotn though is if i need to return a type back to the class that calls the Async method how can I return a value? Also is this the best practice?
You should make your inner private AsyncTask class - static. This is because otherwise it will have implicit reference to your Activity - this means that if your Activity will be recreated - ie. due to config change - then your AsyncTask will still hold your activity reference causing reference leak. This means you should pass reference to your activity to AsyncTask in ie. constructor of AsyncTask, and keep it in WeakReference (WeakReference/AsyncTask pattern in android).
So my question is does that line of code reference the activity the ASYNC Task is called from?
yes, but only if your AsyncTask class is non static
If so I beleve that will be a static method. How do i actually pass in the instance of the class so i can interact with non static functions?
its not a static method, with My_Activity_Class_Name.this you can access non static methods of your Activity class.
My quesiotn though is if i need to return a type back to the class that calls the Async method how can I return a value? Also is this the best practice?
You can call a method on your Activity class, there is nothing wrong with that. Remember that you cannot access UI widgets from non UI thread. So update your Activity widgets from onPostExecute which is called on UI thread.

Pass Variables from AsyncTask back to calling to Activity

I am new to Android Development and have a question (will probably show my newbie status). I am calling an asynchtask from a custom activity. Once the asynch task is completed onPostExecute I would like to call back into my activity and set a pojo (Map()). I know that onPostExecute seems to run on the UI thread but I am not sure how to get visibility into the calling Activity.
The goal is to be able to have some variables set in my activity and ideally the webservice call will already be completed.
Thanks,
Craig
If the AsyncTask is a subclass of the activity, it has access to all public, protected, and private variables of the superclass. If its not, you need to write a public function in the Activity class to set the variables, and call it from the AsyncTask. That will probably require you to pass the activity to the AsyncTask via the constructor and save it in a member variable of the task.

nested Activity class and AsyncTask

I post here because I have a difficult question.
I have a class that extends TabNewsActivity of Activity
This class contains a nested class TabNewsActivity: DownloadData which extends to AsyncTask >>
This class TabNewsActivity displays the recovered data from my web service, a spot DownloadData is asynchronous which allows me to retrieve the values ​​of my web service in a list.
To perform an update values ​​(in my application => refresh) I have to do this:
DownloadData (). Execute ();
But I can not do it out of my context TabNewsActivity: s
I would like a way to re execute this command, but in another tab for example.
Thank you for your help
Suggest making your DownloadData subclass in a separate class file, not a nested class of TabNewsActivity. You can pass it a Handler to act as a completion callback perhaps. This way you can execute DownloadData from TabNewsActivity, and pass it a Handler to call in TabNewsActivity upon completion. The same could hold true when calling it from another class.
AsyncTask execute() method is static so you dont need an instance of anything to call it. You call it like this:
AsyncTask.execute();
That will run what you have in your doInBackground() method. As far as I know you should be able to call AsyncTask.execute() anywhere in your app as long as you import AsyncTask.
Use AsyncTask inside of a Service.

Android AsynkTask problem with HttpUtilities class

I want to tell you about the structure of my classes.
I have an HttpUtilities class, from which I am doing requests(send and get JSON objects) to server. It's a public class with private constructor and static methods.
I have also user class which manipulates data(set string to JSON, and JSON to ...).
Within my activity I have declared the instance of user class.
There are some methods(SignIn, SignOut, InserToListView) there which call corresponding methods of httpUtilities.
Now, I want, that for each event of user, using AsynkTask do request to server and change ui.
I want to call method(for example InserToListview) of user class within new Thread using AsynkTask.
It would be easier if you tried your hand at this first and then asked questions later, but here is some general advice to get you started:
Subclass AsyncTask.
Implement doInBackground, calling your insert method.
(Wherever you are calling your insert method now) instantiate your subclass of AsyncTask.
Call the execute method on your new custom AsyncTask object.
Store your AsyncTask in an instance variable so you can cancel it later on.

Categories

Resources