0

There are a lot of similar questions and therefore this may seem like a duplicate but please read first.

I have gone through all of the following questions and it didn't work for me

  1. onDraw() is not called in custom View
  2. invalidate not redrawing view on api 25
  3. Android Custom Layout - onDraw() never gets called
  4. view.invalidate() not working to redraw imageview
  5. Why onDraw is not called after invalidate()?
  6. Android View Canvas onDraw not performed

Let me clarify further

  1. I am extending View class not any ViewGroup like LinearLayout etc
  2. when I call invalidate() from onTouchEvent it runs fine
  3. when I call invalidate() from another function of the same class (undo()) invalidate() doesn't work
  4. I also tried calling postInvalidate() and also called invalidate using postDelayed(new Runnable ... invalidate() nothing seems to work.
  5. after touching undo button when I touch the view it draws the expected result immediately.

Any suggestion on what could be the issue?

code looks something like below

public boolean undo() {
    if (undoStack.isEmpty()) return false;
    redoStack.push(undoStack.pop());
    if (redoStack.size() >= MAX_STACK_SIZE) redoStack.removeElementAt(0);
    // invalidate();
    postInvalidate();
    return true;
}

here undoStack holds the previous bitmaps that were drawn

I can say that undo and onDraw are working fine as I get the expected results when I touch the view after clicking on undo button. Also I am calling this function from the main Thread and not background thread or async task.

Any suggestions please. Thanks in advance.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
  • 1
    So you have verified, using the log (or the debugger), that `invalidate()` is executing, and `onDraw()` is not executing? I want to make sure you did not draw these conclusions by simply observing the `View` contents. – greeble31 Jan 16 '20 at 16:40
  • It could be because of returning zero as width/height of view in onMeasure https://stackoverflow.com/a/65381220/2700239 – Fartab Dec 20 '20 at 14:57

0 Answers0