View "invalidate" event not called if view not added to activity - android

I have a background view not showed into main activity and I need to get the "invalidate" event for know when something into the view is changed.
The problem is the event "invalidate" is "emitted" by the view only if this view has been added to the main activity thorugh the "addView" call. I don't want to add this view to main UI since is managed in a separate thread but I have this problem of invalidate event not emitted and I don't know how to find a workaround. It seem the "addview" function unlock something that allow to generate invalidate call but still not found the reasons.
Someone know how to force a view to generate invalidate event also if not added to main activity?

I think you can't do this, because invalidate method is called by View framework, so this method would't be called if the view is not add to the window. But I think there's a trick: adding this view to the window and hide it below other view, so this view is invisible and the invalidate method will be called by system.

Why would you want to invalidate a view that is not there? Instead of finding a workaround I suggest to improve how your app works. Invalidate means that you want to redraw something that is on screen. I don't know your exact use case is but my guess is you want to do something that is not UI related in invalidate. You should not do that.

Related

difference between addonGlobalLayoutListener and addOnPreDrawListener

We know ViewTreeObserver is used to register listeners that can be notified of global changes in the view tree. There are two method defined in this class are
addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener listener) and addOnPreDrawListener(ViewTreeObserver.OnPreDrawListener listener). But the the purpose of these methods are not clearly understandable to me which described in the developer site. I tried to understand it and also searched in google but was unable to find clear picture about this topic. Any help is greatly handful for me.
OnPreDrawListener
Gets called just before onDraw() method gets invoked. At this point, all views in the tree have been measured and given a frame. Therefore you can properly manipulate view in this callback
OnGlobalLayoutListener
This listener gets called:
- when visibility state changes. In example when view has been drawn it becomes visible and this gets called.
- when you addView state of view tree changes

Interrupting the onMeasure cycle to display an error (Android)

I've overriden the getView(int, View, ViewGroup) method of View to provide some custom behaviour and I'd like to be able to abort the recursive measure() -> onMeasure() -> measureChildStuff() -> child.measure() if an error occurrs during my custom getView() call, and immediately stop trying to render that Activity in favour of another that shows the error.
Unfortunately, it seems Android insists on finishing the stack of measuring before listening to my Intent to change the Activity. Is there a way I can force an Intent to be immediately started? For example, could I achieve anything by overriding Context.startActivity()?
If you set visibility to View.GONE, layout will skip that view.

listener for contentView

is there a listener or some way to determine when contentView is created? I have used other type listeners and they work for other child views inside of content view. however content view is different. I have not yet found anything that works.
Have you tried onContentChanged() yet?
The documentation says
This hook is called whenever the content view of the screen changes (due to a call to Window.setContentView or Window.addContentView).

In which condition the onDraw method of View to be called repeatedly in Android?

I implement a view by myself, the onDraw method of the view is called repeatedly. This will cause a serious performance problem. The code is so complex that I can't paste here. Anyone can tell me some possible reasons? I haven't touch the view by my finger.
One reason would be that your onDraw method is calling some method that (perhaps indirectly) invalidates the view. Another possibility is something completely unrelated in your program is causing the repaints. Try this experiment: comment out your drawing code and substitute something really simple, such as a simple line draw, just to show that it's working. If the repeated drawing goes away, then it's something in your onDraw code. Just start restoring the current code a bit at a time until the problem reappears and then you can track it down.
If the problem doesn't go away when you try the experiment, then look somewhere else in your code for something (perhaps a worker thread?) that is invalidating your view.
I had the same problem today. I solved it by overriding invalidate() in my View subclass:
#Override
void invalidate() {
super.invalidate();
}
Then I set a breakpoint inside invalidate() and waited for it to hit. When it did, I had a stack trace of the call, and the offending caller. Mystery solved in 2 minutes.
If that doesn't work, trying overriding the other invalidate methods in View.

Does onDraw() calls automatically except on loading and when calling a invalidate()?

I wanted to know whether the onDraw method get called without the programmers knowledge. I know that it is called at the first time View is loading and I know it calls when I call invaliade(). But does it calls in any other times?
Yes, whenever a parent view is redrawing itself like if the custom view is within a ScrollView whenever you scroll it...

Categories

Resources