user5623308
user5623308

Reputation:

How to decide whether to use ASyncTask or not in android?

I have a simple Android UI. When user clicks Button, it takes the user's location and then it goes to 4-5 websites and gets all the events in that hour. Then, according to the user's location, it compares the closest ones, and according to a radius given, it shows the event names in a new screen.

After clicking Button, it will go into another screen and will write something like searching for location or progress dialog, or location identified. After that, it'll show the events to the user. So, should I create 3 activities and 3 screens?

According to this link how to use method in AsyncTask in android?

He says don't prefer AsyncTask for long network jobs.

I can't use location methods inside AsyncTask. Before executing I should send location as parameter. But again, computeDistance method needed. At post execute method, I can post events to new UI.

But when the user clicks these events, from onClick I can do jobs but I can't find or retrieve info of these events.

I decided to use AsyncTask after commenting somewhere here and someone answered me to use but I can't find that post.

And now i am unsure about to use or not.

I need webconnections, so I don't want to make them in main. So it is good to use AsyncTask for that but is it necessary?

Upvotes: 2

Views: 1001

Answers (2)

josedlujan
josedlujan

Reputation: 5600

AsyncTask is an abstract class provided by Android which helps us to use the UI thread properly. This class allows us to perform long/background operations and show its result on the UI thread without having to manipulate threads.

Android implements single thread model and whenever an android application is launched, a thread is created. Assuming we are doing network operation on a button click in our application. On button click a request would be made to the server and response will be awaited. Due to single thread model of android, till the time response is awaited our screen is non-responsive. So we should avoid performing long running operations on the UI thread. This includes file and network access.

Upvotes: 0

kris larson
kris larson

Reputation: 30985

This is what I would recommend:

Use AsyncTask. It will run a background thread and give you a way to display progress in the UI thread as each website is checked. This isn't a "long network job" compared to, say, streaming a video. IMHO, using a Service for something like your operation is just too heavyweight. So start out with an AsyncTask.

Once you have that, however, you will discover your next problem, which is that your web operation might take long enough that if you rotate the device, the Activity will be torn down and recreated in the new orientation. Then when your AsyncTask completes, the Activity it was supposed to call back to is no longer there. Oops, now your user doesn't get their results.

The best solution I have found for that is to use a special fragment to "host" the AsyncTask. This fragment will not create a view and use setRetainInstance(true) to keep the fragment alive during Activity re-creation.

You can read about this novel technique here: Handling Configuration Changes with Fragments

Upvotes: 4

Related Questions