Interrupting the onMeasure cycle to display an error (Android) - 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.

Related

How to detect a moment when ListView item becomes ActiveView (going off-screen)

According to concept of recycling items mechanism in ListView.
I want to know, actually, how i can detect a moment when a View going off-screen.
I explain why.
In most cases ListView have a custom
Adapter(? extends ArrayAdapter / ? extends BaseAdapter, etc.).
getView(...) method allows to manipulate visibility and content of views (text,bitmaps,drawables,etc.)
And in some cases i need to launch a separate Thread which doing background work, and after that update UI. Actually - using AsyncTask.
When i have many items in ListView each call of getView will be produced start a new Thread. I need to cancel them if View is no more longer present on the screen. How to do this?
You can override onDetachedFromWindow for the view.This will let you know when the view is going off-screen.The docs says:
protected void onDetachedFromWindow ()
Added in API level 1 This is called when the view is detached from a
window. At this point it no longer has a surface for drawing.
See Also
onAttachedToWindow()

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

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.

Why requestFeature before setContentView

I want to understand the reason why request feature must be called before setting a view in android activity
From the documentation:
Enable extended screen features. This must be called before
setContentView(). May be called as many times as desired as long as it
is before setContentView(). If not called, no extended features will
be available. You can not turn off a feature once it is requested. You
canot use other title features with FEATURE_CUSTOM_TITLE.
requestFeature modifies the window that you are inflating a view on. The window must be set before you can inflate a view on it which is what setContentView does. In layman's terms, you wouldn't try to hang blinds before you put the window into a wall right?
When the view inflates it needs the window to be set and stable so it can properly calculate where to put items. If the window is not constant then the content view would have to be reinflated after every requestFeature. If Android did not enforce the rule of requestFeature before setContentView, on every requestFeature the view would be reinstalled and the end result would most likely be the screen flickering a few times as the view gets reinflated time and time again.

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

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