I have a form with about 100 views and apparently all the findViewById is taking a long long time. And i need to do a save and retrieving of the form fields which takes around 2-3 seconds freezing the UI thread.
Is there any way to make it look better? I read that its not feasible to call findViewById or any UI related stuff on another thread but i have no choice.
Saving the forms from that many field is taking alot of time as well.. im doing it on another thread currently.
On honeycomb 3.1. please help. many thanks.
You might want to rethink your design, perhaps you could use a ListView, which'll only construct enough views to fill the screen, rather than the entire UI. You could also use the events from the edit controls to update a structure with the current values as they are edited rather than waiting til the end then trying to read all the values in one go.
Related
From Activity lifecycle we know, that in onResume the UI is visible. But if I set a breakpoint on onResume, I still don't see it, only after that. I heard there's some method which can check it, but I can't remind how it calls. We used it to make better animation. So how can I be fully sured that UI is ready?
Add global layout listener on view you want to check. Also please make sure that you remove listener once your work is done else it will keep getting called multiple times.
#onik shared a good link that should solve your problem
This ensures lay-outing. Drawing happens continuously and frame are refreshed according to sys clock.
I recommend watching: https://www.youtube.com/watch?v=Q8m9sHdyXnE
This will give you good idea on android drawing and layouting
I have an activity indicator in my app, which I show when I'm processing quite a lot of data (building table rows) in a for-loop.
However while in the loop, the indicator doesn't move at all. Probably because the loop uses all resources.
Do I need to put a kind of 'sleep' in the loop so the indicator can redraw, or is there any other solution?
I'm using Titanium sdk 6+, the app is written with Android in mind.
Thanks in advance!
Actually, here you are doing 2 different UI tasks on one & only 1 UI Thread.
UI Task 1: Showing indicator
UI Task 2: Showing table rows.
But there's only 1 UI thread which we call as Main UI Thread. So doing 2 tasks on same thread will keep hanging up 2 tasks interchangeably which is why you see indicator moving sometimes after a freeze.
Ideally, it doesn't take much time to create any UI with today's processors so that you need to display indicator while doing them.
If you still think that your UI is taking lot of time in building up, then I would suggest you to choose another structure to create rows because as per my experience, creating 100 rows similar to Twitter Tweets on an average device will only take 2-3 seconds if built up properly.
That's why you really need to consider redesigning the Row UI creation code & it's strictly not recommended to show indicator on UI tasks.
To help you more, share some code snippets of how you are creating rows & what type of UI elements you're filling in them.
I'm curious guys,
What are the exact cases doTraversal -> performTraversals is called? Since I have pretty heavy Activity, I want the application to call onDraw or the sort. performTraversals is pretty heavy, it is trying/measuring if it should resize views, stuff like that I guess. And I don't need it when I'm making some view, that no other view is dependent on, GONE, but I guess I can't skip that. So before digging into the source of Android, I want to ask. If you know it, please feel free to share :)
Thanks,
Danail
performTraversals() has many jobs but its three main roles are:
Measure views
Layout views
Draw views
Every time Android needs to redraw a window, performTraversals() is invoked. It does not mean however that measure/layout happens every time performTraversals() executes. You cannot skip performTraversals() if you are using a standard views (only SurfaceView lets you bypass this when drawing.)
I'm reading up on SurfaceView and how to use it, and I've come across some information that states that a SurfaceView has View#willNotDraw() set to false by default, and that it's up to you to call SurfaceView#onDraw(). I also read that RomainGuy said that this is done by default because it is more efficient. My question now is, when should you handle calling SurfaceView#onDraw() in a separate thread, and when should you just set View#willNotDraw() to true, and just call SurfaceView#invalidate(). Is there a difference between the two, and does one improve performance more than the other?
See:
http://developer.android.com/reference/android/view/View.html#setWillNotDraw(boolean)
I'm not sure where you got your information, but at least the javadoc says that most users will set this to false to get Android to send it onDraw events itself. As for your question about when you should do this, I would say it comes down to why you're using a SurfaceView.
If your view is displaying something dynamic (e.g. for a game or something that has a tight event loop), you'll want to be controlling exactly when updates happen, especially if you'll have the information to use one of the more detailed forms of invalidate to save redrawing the entire View. You won't want Android to call invalidate for you, and that's why the flag is there.
If, on the other hand, you are simply drawing something static, it makes sense to let Android's UI stack control the invalidations.
By the way, invalidate only posts a request to re-draw the View, so be aware of this if you intend to use the event-loop style (onDraw will be called sometime after you call it).
Edit: some clarifications.
Using SurfaceView.onDraw() and SurfaceView.invalidate() will make SurfaceView behave like a normal View and you will pay for the extra overhead associated with SurfaceView. If you want to draw from the UI thread, use a regular View instead. It's easier and cheaper.
I'm developing an Android 2.2 application.
I have an event listener on an activity, and I want to set visible a TextView when I receive an event. But there is an error:
I only can set it visible from UI thread.
In C# and Windows Mobile there is a BeginInvoke. Is there something similar in Android?
Thanks.
You can use Activity#runOnUiThread or an AsyncTask as the two easiest ways to duplicate the BeginInvoke functionality; with runOnUiThread being the one most similar.
For more complicated or performance orientated needs (i.e., you do not want to keep creating a large number of Runnable objects) you can use a Handler. However, I do not recommend it as your first choice.