In my main activity A, I have a button, when click, it will go to another activity B. when b on create, it queries from a table which is about 200 records and appends each record as textview into a ViewFlipper.
I have few problems. First, when the button in main activity is clicked, it takes about 3-5 seconds before go to activity B everytime.
Any suggestions?
Run some traceview to see what is slow, or manually add time stamps. It depends on how the query and schemes are setup, but if the count of the table is only 200, then the should maximum take a few milli seconds.
If it is the case that you create and layout 200 TextViews in onCreate, then i would think that is the reason for why it is slow.
Run your query in a separate thread or consider using asynquery ?
I think AsyncTask or Thread are better option for getting the details. Show progressbar when doing background processing.
This example simulates your problem.
AsyncTask basic Example : AsyncTask
You need to use asyncTask to get 200 records. It runs query in background
Related
I have a activity that show one listview. In the activity I have a one AsyncTask (named here of AsListView) to get values from internet and fill some informations in each item of the listview. Work fine.
Now I created a button in ActionBar to show one image from streetview. To do this I have implemented another AsyncTask (named here of AsImage) to get image from google and show in a DialogFragment, but is necessary wait the execution of all AsListView Threads. It spends long time depending of the number of items in the list.
To execute AsImage rapidily, I cancel all AsListView tasks, but it's not good for me (user loss informations). The ideal soluction is set AsListView tasks to wait while AsImage execute. When AsImage finish I set AsListView tasks to continue execution. But I know that is not possible handle the control of execution of AsynkTasks.....
Some solution?
You can try to synchronize the AsyncTasks using a CountDownLatch.
Otherwise if you want you can use Threads instead of AsyncTasks and set their priorities, but there is a reason android made the AsyncTask class so I recommend you use it.
I am using asynctask to do lengthy operations such as DB reads. I want to start a new activity and render its contents when all the asynch tasks are done. What would be the best way to achieve this?
I know that onPostExecute is used to update screen. But I dont want to update screen unless all ther ayncTasks are done
Please help
Thanks
Don't overcomplicate it: just use a counter that gets incremented in every onPostExecute. If a task finds this counter to be maxed (equal to the number of tasks you spawned), it can run the update code.
Note that you don't need locking around this counter, since onPostExecute runs in the UI thread.
Just a suggestion, You can create an array of boolean that is sized to the number of Asynctask. Each Asynctask will have an ID which is index to the boolean array. In onPostExecute or other callback, you can check if all of the entry are true. Then you know it is all done. Another way is that, you assign unique id for each one of the Asynctask, and you also have the sum of those id, when one of them is done, you subtract its id from the sum, when it reaches zero, you also know they are all done. Not perfect, just some suggestion.
I know it sounds crazy that someone is using runOnUiThread inside AsyncTask. Somehow, it is working for me but I wanna know if it is an acceptable and robust approach or not. Here is the scenario:
I have an app in which after successful login, user is rendered to next screen. In this new screen, 3 different methods are loading different types of data from a web server. These methods are:
getMembersForList() : It loads the list of community members and shows it in a listview.
getProfileData() : It loads the profile of logged in user and shows his name , image etc on the screen.
getNotificationCounts : It loads the count of new notifications for the user.
I applied 3 different approaches for it :
(1) Calling all 3 methods simply in onCreate i.e. no exclusive thread is being used for any of the methods . In this case , the transition from login screen to this screen becomes very slow and black screen shows up for some time before this activity shows up.
(2) Calling getMembersForList() on UI thread and the other 2 methods on exclusive threads. In this case transition becomes fast and list shows up quickly but Notification counts and username etc. don't show up because WrongThreadException occurs saying that this thread can't touch other thread's views (TextViews for username, notification count etc. which are declared globally) . The same thing happens when I start these threads from an AsyncTask as well.
(3) Calling getMembersForList() on UI thread and then starting an AsyncTask in which the other 2 methods are being called in "runOnUiThread" inside doInBackground() method. This solves both the above issues. Now the screen transition is faster and the WrongThread exception is also not occuring.
So far the approach-(3) is working good for me but I am not sure if this is the right way to do it because runOnUiThread and AsyncTask are 2 completely opposite things. Can anyone please clear my doubts about this scenario. Thanx in advance.
Yes, use-cases like this are a big reason why the runOnUiThread() method exists in the first place. The idea is you allow your background thread(s)/AsyncTask instance(s) to run your lengthy operations in the background, and then provide a simple hook that they can use to update the interface when they have the result (or at arbitrary intervals, as different pieces of the result become available).
As long as that's what you're doing, then your usage is fine. What you want to avoid doing is performing a lengthy operation on the main thread, either directly or indirectly by passing in some lengthy operation from a background thread.
Of course you don't have to do it that way if you don't want to. You could use postExecute() instead. Or you could store the result somewhere and then use any sort of message-passing API to notify the main thread that the result is ready, and so on.
I would advice to run all the 3 calls in the asyncTask, and update the UI in the postExecute() of the AsyncTask after the background taks is complete, postExecute runs on UIthread so you need not call anything explicit to run them on UIthread.
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().
I have three activities
1 is the main activity
2 is some other activity that does something
and 3 is some other activity that does some storage management with sqlite
in this 3 activity I have a function that updates some data on the database's table
I want every 10 seconds to call a function in order to update this table
ScheduledExecutorService can do what I want. However I'm not 100% sure. If I'm on the 1 activity will it be able to call the function from the 3 activity even though I'm on the 1?
I also found alarmmanager but I don't want the update of information to be happening even when I close my application, I want it to happen only when I have the application open!
thanks!
You can use TimerTask or Handler. Here is a tutorial:
http://developer.android.com/resources/articles/timed-ui-updates.html