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

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.

Related

Android, xamarin: Load RecyclerView when Activity is already visible

In my activtiy, I am loading a recyclerview that downloads pictures from the internet.
The only problem is, that the acitvity will only become visible when the loading of the pictures is done. I call the function from within the OnCreate() method. Since this made it obvious, I decided to put the function into OnWindowFocusChanged(), yet still the activity will only start showing when the pictures are loaded. (This is like a 1 second delay, but its a litttle too much.)
Where would I call my InitRecView() method to make sure it will start loading once the activity is already visible to the user?
Thanks
You are getting this problem because you are loading images on main thread. It may also cause skipping layouts.
Here is the solution: Use Handler or Async task for download images, show progress till then and once image is downloaded then notify adapter using adapter.notifydatasetchanged().
note:make sure you write UI related code in runOnUiThread since you cannot handle UI elements in background task.

Call function 1 time after layout is inflated

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.

Android:ListView not displaying items when power button locks the screen during AsyncTask

I have a Async Task that creates a HashMap to create a Adapter to populate ListView. I have a progress dialog that shows during doInBackground method.In onPostExecute() method, I dismiss the progress dialog and call a method that populates my listview with the list of items saved in doInBackground method.
This works fine. But I noticed something strange:
The issue I see is, if I lock the screen when the progress dialog is about to be dismissed (in onPostExecute), the listview does not display, even though it has non-empty items in it. I verified it in logcat messages and when I debugged.
Is there a possibility that a screen lock (I do this my pressing power button once) blocking UI thread? How can I resolve the issue and make sure ListView displays its items?
Code for onResume():
#Override
protected void onResume(){
super.onResume();
if(MyAdapter !=null){
pull_listView.setAdapter(MyAdapter);//pull_listView is listview
MyAdapter.notifyDataSetChanged();//MyAdapter is the adapter
}
}
This situation illustrates the downside of using AsyncTask to do background processing. In some cases, an IntentService may be a better choice, especially if you think the background work is going to take some time. An AsyncTask ties the background work to the current Activity, while an IntentService is completely decoupled.
The Android training class Running in a Background Service shows you how to set up an IntentService, request work, and notify your Activity when the work is done. Passing data from the IntentService to the Activity is a bit more complicated, but there are options.
onStop() will be called when your screen goes out as per the Activity Lifecycle. You could override onStart() or onResume() and put a check in there to see if your data has been populated. If not, populate. You may even want to overide 'onStop()' to save data if the screen goes out.
Edit
In this particular situation, I would think onResume() or onStart() would be fine but onResume() is usually the safest because it is guaranteed to get called before the Activity is shown as illistrated in the link I gave. What kiind of a check you want to use is up to you and depends on how you handle evrything. However, if your AsyncTask is an inner class of your Activity class the you could simply create a boolean member variable, say boolean isDone=false; change this to true in your doInBackground() or onPostExecute() then your onResume() knows the data Is loaded. If its false then you can try to get data again. Hope this makes sense and can help

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().

When to load database data into an adapter

I have a simple personal application I'm working on that queries some records in an SQL Database and populates an adapter for a listview and is basically working fine... but I've began to wonder if I'm doing certain things at the right point of the framework.
Currently I'm loading everything up during onCreate(). In theory, I could be loading up quite a bit of data, so I wanted to possibly throw up a ProgressDialog while the information is being added to the adapter, but I ran into some odd threading issues with the Cursor. Ultimately, I launched a Progress Dialog near the end of onCreate(), followed by sleeping on another thread and calling a method to load my data with runOnUiThread() following the short sleep time, having the end of that method dismiss the Progress Dialog.
This works, but it's brought me to whether or not I should be loading database data during onCreate... or whether it should be moved to onStart() or onResume(), adding in code to clear the close and open the database, clear and repopulate the adapter as necessary as other Activity's are started and finished. Or would all that be unnecessary and I should just keep the adapter populated during onCreate()?
Reto Meier's suggestion to use an Application may suit your needs. Take a look at Activity restart on rotation Android
Move it to onResume, as if you stop the activity you can destroy the adapter and fill it back when to resume the activity.
It helps to save memory and also helps to update the adapter if data has changed.

Categories

Resources