Is it possible to execute onDraw() right after invalidate(Rect)? - java

I added a breakpoint in my onDraw() method and noticed that my debugger only gets there after the method in which invalidate(rect) is, ends. Is it possible to be called right after?
Because I need to call invalidate(rect) twice in my method.
Thanks in advance.

No. Calling invalidate() is a signal to the framework that a portion of the screen is dirty and needs to be redrawn. The actual drawing doesn't happen immediately... it happens the next time through the main thread's event queue.

I have faced such problem with me and as alex saying it'll not happen. I can suggest one workaround that u can have your own implementation of onDraw() method with different name like myOnDraw() only when it should be not very heavy. so instead calling or depending upon onDraw() to be called immediately u can call that method which will work for u when u need it.

Related

instantiating a second class, will java wait till the second class's constructor is done, then move to the next line of code?

okay, i hope i can explain my question so its not hard to understand,
i want to know if i can create an instance of a class in my main class but im not sure if the constructor of the second class lets my main class continue while the second class is busy with its constructor.
the reason i want to know this is, i want to access the data of the second class on the second line of my main class (right after the second class has been initiated)
sorry for this weird question, i hope i didn't too bad explaining my question (im fairly new to it)
thanks for reading!
Unless you explicitly create multi-threading (by using any of the various ways Java supports for that), all the code runs in a single thread, so calling a constructor will always wait until the constructor is done.
will java wait till the second class's constructor is done, then move to the next line of code?
Yes. Your application will run in a single thread by default (though it is possible to change this behavior). This means anything that happens in your code - method calls, loops, any calculation - they will occur serially; one thing after the other.

Can I flush all pending onDraw calls

I was using invalidate() to try and make something draw on the screen... but invalidate() says:
onDraw(android.graphics.Canvas) will be called at some point in the future
So I guess I could find some way to call the onDraw() myself, but does Android offer any sort of flush function that can be used here?
Searching around so far the only thing I found was the flushable class, but I'm not sure if that can be leveraged somehow.

paintcomponent repeats itself without any user interaction

I'm working on a project which uses paintcomponent.
The problem is that this paintcomponent method is executing itself repeatively without asking.I discovered this problem by creating a counter that raises the method runs everytime and I printed this out. Now is see that the method repeats itself randomly.
The problem is that this makes message boxes etc execute multiple times and freeze.
How can I solve this?
paintComponent is a low-level method which can be called at any time at the discretion of the GUI engine. It is not a place to instantiate any message boxes or similar, but to use low-level 2D graphics calls to paint your custom component. Your use case may actually call for a different mechanism whereby to refresh your screen.

glReadPixel doesn't work in OnPick function

i try to implement 3D picking in an android app. I use OpenGL ES2.0.
Now I want to read the depth buffer (which is written in an FBO) but glReadPixels returns only 0.
The code is correct, because I tried the same code after calling my RenderInTexture function and it returns the correct values.
Calling RenderInTexture in the OnPick function and than call glReadPixels returns also 0.
What is the problem?
Isn't it possible to read a framebuffer in the OnPick event or do i have to set some variables?
Thanks for help.
Is the OnPick called on the main (UI) thread? Probably you have two threads, the opengl rendering thread and the main application thread where events are sent. Try storing the onpick coordinates in you OnPick method and do the computation in the opengl thread.
Hope that helps.

Android OnItemSelected of a Spinner is triggered after several other methods finish executing

I have been going through the threads regarding the spinners and when the onItemSelected is triggered. I concluded that it is triggered whether you manually select a spinner item or your programmatically select an item through the spinner.setSelection(position) method.
My problem is that OnItemSelected callback method is called first within my code, but it is executed after several other methods finish executing. This is an undesired behavior because my code logic depends on certain steps being executed in sequence.
if(conition is true)
fillSpecialtySpinner();
if(another condition is true)
fillSubSpecialtySpinner();
The fillSpecialtySpinner() method contains the spinner.setSelection(position) line of code. But somehow it is executed after the second if is checked and the fillSubSpecialtySpinner finishes executing.
Any help would be appreciated. Thank you in advance!
If I understand correctly, you are wanting to make sure that fillSubSpecialtySpinner() method is only called after specialitySpinner.onItemSelected().
Can you just call fillSubSpecialtySpinner() from within specialitySpinner.onItemSelected()?
The callback doesn't happen until every other function is executed, yes, ad Baqueta said.
So you can't do something like set a boolean to false, make the change to the spinner, (execute code in onItemSelected if boolean is true), then set it to true, because the function will get called AFTER your boolean becomes true.
However, I have an easy and generic solution to the problem (Refer the accepted answer to the question):
Undesired onItemSelected calls

Categories