Android - when is performTraversals called? - android

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

Related

How can I track if UI is fully drawed in Android?

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

Android different way of doing onClick

Im looking through a project here and it has a way of using onClick that is different to what ive seen any other time.
Usually you set the listener for the button during onCreate or whatever.
Here in the activities xml it has android:onClick="navigateToUrl". Then this seems to kick off the method navigateToUrl in the classes code.
Im just wondering what is the difference between the two methods?
They work the same way. With the xml version, the framework adds an onClickListener during inflation that uses reflection on the Context its called from looking for a function with that name, and calls it. So its slightly less efficient, but not enough to really get worried about. The big advantage is a less cluttered onCreate, the big disadvantage is that to figure out what a view does when clicked you have to read xml rather than code. Which you use is a matter of personal preference. I'm currently in the explicit onClickListener group, because I prefer not to have behind the scenes magic.
For the latter, you need to keep a public method always. If you do not want to keep your method publicly visible, you would prefer to have a listener implemented.

What is equivalent of onDraw in iOS

On android we have onDraw(). What is the equivalent in iOS ?
You probably want drawRect:, though depending on what you want in your view there might be other options of interest (subviews & Core Animation layers). See the View Programming Guide.
If you're writing a custom view, it's basically -drawRect: which gets called on the view every time the system wants to redraw (e.g., every time the runloop turns and -setNeedsDisplay flag is set.
you override the -drawRect method of a UIView to do direct drawing to the screen. However this isn't commonly needed for lots of use cases. If you want, provide more detail about what you want to achieve. There may be a more iOS way.

When should you let SurfaceView be drawn by UI-thread?

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.

Taking a long time setting up views

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.

Categories

Resources