Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call an Activity method once every frame on Android?

Tags:

android

I wish to sometimes (when a flag is on) update a few UI components in an Android activity every drawn frame (i.e., not while app is not visible, and not more than once per frame). How do I do this?

like image 475
Blixt Avatar asked Sep 19 '15 21:09

Blixt


1 Answers

The question specifically states "every drawn frame", which suggests that overriding onDraw() in whatever it is that's being drawn will solve your problem exactly.

If that's not quite what you're going for, you should take a look at Choreographer, which invokes a callback once per display refresh. You must renew the callback every time it is invoked. To stop the callbacks when the Activity is in the background, establish the callback in onResume(), and set a flag in onPause(); if the callback sees the flag set, it doesn't renew itself.

The documentation for Choreographer notes higher-level API features that do common things. If one of those fits your needs, prefer that over the use of the lower-level API.

like image 170
fadden Avatar answered Oct 15 '22 22:10

fadden