where should I do Download my data in android - android

I want to download the data from the web service, actually at the first time I just download it by the asynctask (background thread)and I execute it in the Oncreate method, this work fine.
but I also try to download in the ui thread in the oncreate method, so I just call the Httpclient directly in the oncreat method and this work fine and also faster,and the application doesn't fail when I press on the screen when it download the data.
I think the second method work fine because the application will not show the user interface until it finish the oncreate method.
Is this correct ? should I use the second method ?
the first method :
oncreate(bundle saveinstance){
new Task.execute("url");
}
second method:
oncreate (bundle saveinstance){
makehttprequest("url")
}
THANK YOU VERY MUCH

No, you should not use the second method. On newer versions of Android doing any network processing in the UI thread will cause an exception to be thrown. Given the indeterminate amount of time a network call can take, doing those calls on the UI thread makes for a non-responsive UI that will open up to a blank screen for as long as it takes to complete the call. Use the Async task and dhow a progress bar or a splash screen indicating to the user that you are downloading some content.

Related

Does Volley execute UI thread operations when my app comes to the foreground?

I have a button that calls requestQueue.add method of the Volley library. Inside the onResponse method i call:
popupBox.display(getString(R.string.successfulRegistration), false, true);
Display method, displays a popup window thus must be called inside UI thread.
I put a break point at the beginning of the onResponse method.
Immediately after clicking on the button, switch to another app and send my app to the background.
IDE stops on the break point and I get my answer from the server but my app still is in the background and there is no exception error.
After about a minute, I bring my app to the foreground. After that, the message window pops up.
Is that mean, I don't need to check if I can do something related to UI thread inside the onResponse method because volley handles it?
onResponse and onErrorResponse are called on main thread by default unless you are trying to call them from some other tread. Volley makes the Api call on a non ui thread (non UI blocking). Volley doesn't take care of your life cycle. So, if you make a api call in an activity and close that activity, the callback are received. So, you should check whether the activity is in foreground or not.

Does OnResume() in Android makes Activity loading faster?

I was developing an Android Application where I have to load a large data from Android Database and view it on screen.
In earlier stages of development I used to retrieve data from DB in OnCreate() method and it really slowed the start of activity when data became huge. So I called the retrieving of data and viewing it on screen in OnResume() method, so that my app does not crash while taking too long to load it.
I know activity is not shown until OnResume is completed.
So is it a good approach to call time taking operations in OnResume instead of having all initialization in OnCreate() method?
So is it a good approach to call time taking operations in OnResume instead of having all initialization in OnCreate() method?
no - you should not do it inside OnCreate or OnResume. You should always do it inside working thread - like AsyncTask.
Any opertion that takes to long to finish, if executed on UI thread will block message queue - this means there are no repaint of UI widgets, user cannot tap on your app to do anything. Everything is blocked until your operation finished. After ~5s Android generates ANR (application is not responding) and kills your app.
You should never do IO operations on the main thread. This includes onCreate and onResume methods, which will always be called on the main thread.
To correctly load data you should either use an AsyncTask or the ContentLoader with its callbacks.
Generally speaking, to use a database with your application you should use a ContentProvider and CursorLoaders. There are plenty of tutorials on how to use these and it makes your code better manageable.

prevent aynctask getting killed before completion?

in my activity, i have an asynctask that downloads a series of images...(it may take some time depending on the size of the images)... before i execute the asynctask, i display a progress dialog in onPreExecute and a notification (when user clicks on notification it is redirected to my activity with the progress dialog). when my asynctask completes, in onPostExecute, i remove the dialog and the notification.. i handle screen orientation by using onRetainNonConfigurationInstanceto save my asynctask so that when screen is rotated, i check if task is still running and i display the progress dialog if it is still running in onCreate
Problem : sometimes: my asynctask downloads only 1 file and it seems that it gets killed...(no exception in logcat)... as a result, my notication is always there and my progress dialog also... and they remain there indefinitely....
i have tried the solution by Jeff Axelrod there: How can I ensure an AsyncTask is completed before my activity is killed?:
It looks like if I override the onPause() event in my activity, and from within the overridden onPause(), I call cancel(false) on the AsyncTasks, then the activity is not destroyed until the AsyncTasks are completed.
seems to do the trick but problem is that my onPostExecute is not called anymore; all images download fine but as onPostExecute is not executed, notification and progress dialog still remain there forever.
waiting for your solutions guys! i read use asyntask only for short task; will the use of thread and handler solves my problem? will android kills my activity or thread if the latter is not finished??
Best way how to handle Asynctask is described in this article.
In short, the idea is to keep AsyncTask in fragment with setRetainInstance(true); these will keep You AsyncTask alive all time user is in activity holding this fragment and won't be destroyed on configuration change (orientation change).
If You need Your AsyncTask to run after user leaves Activity, for example goes to next Activity but You wish download to continue You should use services.

Logging in Activity, postOnExecute, Resume

I need to implement auto-login process from an activity. Also I need to show ProgressDialog while logging in. I use AsyncTask for login process, because I haven't succeeded to show ProgressDialog in other way, and I execute the mentioned AsyncTask in onCreate. I read that all the code which is executed after doInBackground must be written in onPostExecute, but I have code in onResume. The onResume is launched when AsyncTask hasn't finished its execution yet. Is it possible to launch onResume only after AsyncTask is finished? Or is it possible to execute other functions after AsyncTask is finished (in general)?
Is it possible to launch onResume only after AsyncTask is finished?
No, you need to follow the Activity Lifecycle. And the AsyncTask is asynchronous so that it can run while your UI can still do other things. What you can do is put the code that is in onResume() that you want to run after the task has finished in your onPostExecute() method. There are other options but without seeing the code you are trying to run, this would be my suggestion.
Or is it possible to execute other functions after AsyncTask is finished (in general)?
Yes. You can put that code in another Activity method and call that from onPostExecute()
You also can use getStatus() method of AsyncTask to run code based on the Status of your AsyncTask
Short Answer: No.
If you check the developer android website or this image, you'll check that OnResume() after the Activity is loaded, either after created, paused or restarted. In my case, I have that verification done in OnResume().

Long operations on Activity's onCreate()

I have an Activity that retrieves information from a remote server and displays it in a TableLayout.
The function that retrieves the information from the server has its own timeout, and exception is thrown when the timeout gets exceeded.
Now, when the activity is loaded, I want the function to be fired, and a progressDialog to be shown while the function works, and to be hided if the function is done working, or if a timeout exception was thrown.
The problem: I've put the code that do all the functionality described above in the onCreate() function. Nothing is shown on the emulator screen, since the onCreate() function hasn't finished running...
I've also tried to put the code in the onStart() function... same unwanted results...
I'm trying to avoid using of threads, because the functionality needs many variables that the thread will not has access to them...
How can i implement the wanted behavior??
Thanks.
Use AsyncTask with ProgressDialog bounded:
http://it-projects.spb.ru/?p=150&lang=en
Create a class implementing Runnable and put all your load logic in there. Call a function in the activity when finished (lets say onFinished(params...))
Create a UI Handler and get the handler to update UI in onFinished(params...)
Create a thread in onCreate and start it there to call your Runnable.

Categories

Resources