Call function 1 time after layout is inflated - android

I'm making an android app that is pulling a list of objects to be displayed on the screen from a REST service.
What I need to happen is a progress dialog show while the results are being fetched. The issue I'm having is that I'm not sure which function to put this in. I've tried OnWindowsFocusChanged but it's being called repeatedly and the progress dialog doesn't pop up. I've tried onCreate, onStart, onResume but they're all called before the view is inflated so nothing renders.
Does anyone know how this is done?
Thank you for any help,
Brad

Alright, first of all those apis wont work for you, what you need is AsyncTask,here is what you do,
First implement AsyncTask, put all your downloading code into doInBackground(), once done.
display a progress bar, right before you exceute your asynctask calling task.execute(..)
once your downloding form rest is complete, return your result from doInBackGround
Now control comes to onPostExecute of your AsyncTask, after populating your list, just dismiss the progressbar.
If you wish to indicate downloading percentage to user, you can do same via onProgressUpdate API to which you can push, update via doInBackground.
for more info, read http://developer.android.com/reference/android/os/AsyncTask.html
Hope this help.

Related

Android application being too laggy

My Android application is running very slow and lagging much. I have PHP API on my server and my application requests data through HTTP.
Though, the problem is that sometimes I should wait for few seconds before I can see the result. I have all calculations done in the main thread in onCreate (parsing XML, adding controls) and downloading data from HTTP server in AsyncTask.
How to optimize my program to make it faster? I want it to load activity first and only then, in background, download and parse data. How is it possible? Sorry for newbieship.
what did you mean by lagging ? Will you elaborate more on the issue.
One suggestion that remove parsing XML from OnCreate and move it to AsysnTask. The reason for this is as you are doing time consuming operation in UI Main thread which will impact the activity to be shown.
Create thread to perform HTTP related operations and parse the response on the same thread and while doing the parsing operation show dialog.
Dismiss the dialog when parsing got completed and then show the activity which you want to display.
In the doInBackground() method in AsyncTask add the data parsing and create the data objects then in onPostExecute update the ui elements.
The reason to do something like that is for the application to be responsive in all this time and just make small jobs on the ui thread so as not to freeze.
You can instatiate your views to a default state an add a progress somewhere on top to indicate that the activity is currently loading. For example you can create an empty ListView or a Button that cannot be selected and when the parsing is done then you should set the adapter to the list and make the button back to selectable again.
All this things can be implemented according to what you want the user to be able to do in the time of his waiting.

Android - How do i execute code *after* a process is loaded

Basically when a menu item is pressed in my app it takes the user to a new screen.
But now i want to get a little bit of data online(a time/date) that will display somewhere on the page once it gets the data.
At the moment the new screen doesnt load until the app gets the data which leaves a couple of second pause instead of loading the screen instantly and then showing the downloaded data when it finally has it.
Ive tried using the onStart method since that gets called after onCreate so i thought the page would be created and displayed and then onStart gets called but that doesnt happen, theres still lag between it loading because its getting the data online and then displays the page with it already loaded.
How can i sort this?
Thanks
For any kind of loading operation you should consider using the AsyncTask in combination with the ProgressDialog in order to inform your user that you're performing something in the background.
Remember that you can only interact with the UIThread in the onPreExecute() and onPostExecute() method of your AsyncTask.
So in your case you'll probably want to fire the AsyncTask in the onCreate() method of your Activity, showing the ProgressDialog in the onPreExecute() method and then showing your information and hiding your ProgressDialog in the onPostExecute.
It's pretty much impossible to eliminate this lag you're talking about. So you better just handle it appropriately.
Hope this helps answers your question.

Display loading screen using AsyncTask with ListActivity

I've got an app that uses ListActivity to give users a list of actions. When they click one I use an Intent to launch a separate activity.
My problem is that the actions that the app performs take about 20 seconds to finish, and since I don't want the user to receive that nasty ANR dialog, I tried to use AsyncTask to present them with a loading screen in the mean time. I tried using setContentView(R.layout.loading); on onPreExecute(), but it throws a NullPointerException which as far as I have figured out is due to the fact that loading.xml is not "a ListView whose ID is android.R.id.list".
So what can I do now? How can I show that loading screen? Is there a way around this pretty annoying situation? Any help would be greatly appreciated. Thanks!
I am not sure exactly what your use case is; you have a list of items that are populated immediately, and upon selecting one an action is taken? The action that is taken is to launch another Activity which performs background processing?
Or does it take that long to populate the list of actions?
If the former, you can use an AsyncTask for the long-running activity instead of an Intent to launch another Activity: in the callback you get for the click on the item in question, you would create the AsyncTask, and in doInBackground you would perform the long-running activity, with onPostExecute refreshing or manipulating your list as necessary.
Another thing to consider is using a dialog box to show a loading screen, if the loading is required to happen before you launch a new Activity.
If you can further describe your use case, I can help you more.
It's not the loading screen you need to have on the AsyncTask, it's that 20-second Activity initialization. I would look for a way to do all the setup in a background thread in a Service while the user is free to merrily bop around in other Activities. I'd try hard to find a way not to just stall the user for 20 seconds. Maybe take them to the target Activity and show them data cached from their last visit until the new set is ready.
Fire up and display your loading dialogs in your onCreate() of the Activity being called, then call Dialog.dismiss() in your AsyncTask's onPostExecute().

Asynctask API call issue

I'm currently using an AsyncTask to make an API call and populate a list with data.
I have a Sub Menu whose items can call the AsyncTask to populate the data, problem is that if i click quickly i end up with merged results obviously cause the AsyncTask is running at the same time as each other.
What is the best way to handle a situation like this? Sorry if this is a amateur question.
I would use a ProgressDialog to show that content is being updated, and when the update is complete, dismiss() the dialog. While this is happening, you should make sure that you are not accepting touch input on your ListView. (this may happen by default when the ProgressDialog is in front, I am not remembering currently...)
Take a look at this link for an example.
You could configure only a single Asynch task to run at a time.
A boolean variable which is set to true as soon as the asynch task starts and set to false as soon as it finishes.
Next call could wait for this to be set to false. You could also rate limit what is the min time after which only making the API call makes sense.

working with asynctask and json parsing simultaneously between two activities

I need some suggestion on how to go about this specific android problem:
activity A passes an intent to activity B,
B reads it, makes an API call,
B receives response, parses it, and updates its views from the response
now it works fine except fr a 2 second black screen during transition from A to B
is asynctask a solution? becoz the parsing data is not much and also is there a way to update UI views frm asynctask
To echo what Lalit said, an AsyncTask will help in this case. The problem that you're having is that doing the API call during onCreate is blocking the activity from updating the UI. If you use an AsyncTask, it will allow the activity to continue rendering the view.
As you're parsing the result, you can update the UI by calling Activity.runOnUiThread() or by putting the UI code in onPostExecute.
I'm not sure if AsyncTask is the best solution for your case.
An AsyncTask is very helpful if you need to do something in background and allow the user to do something else in the mean time.
I'd sugest to just use a Thread and define a progressDialog.
If you do it this way, your user will see the loading dialog with the spinning circe and not the black screen that looks more like if the App is freezing.
Let me know if you need help with some code samples.
Marco :)

Categories

Resources