Issue
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?
Solution
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
Answered By - kris larson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.