android – Redraw path and paint on canvas after canvas.drawColor(Color.white)

I have a simple whiteboard type view where I would like to store all the of lines drawn, clear the canvas and then re-draw all of the stored lines. In other words, something like the following should happen:

  1. The screen is completely white
  2. User draws a black line
    • A bunch of paths (Path objects) that represents the line the user drew is drawn to the canvas with canvas.drawPath(path, paint)
    • The same path and paint are stored as a custom Drawable (DrawablePath) in a list of Drawables
  3. User clicks a button to erase the whiteboard
    • canvas.drawColor(Color.WHITE) is called
  4. User clicks a button to draw all of the erased lines
    • canvas.drawPath(path, paint) is called for each of the paths stored in 2.

To achieve this, I’ve tried the following. In the view’s touchMove() I have the following code to draw and store the custom Drawable:

    extraCanvas.drawPath(path, paint)  // draw immediately
    doc.add(DrawablePath(path, paint)) // store path in doc

where doc is a Drawable with a list of Drawables. The doc’s draw(canvas) method loops through its list of Drawables and calls draw(canvas) on each of them:

    override fun draw(canvas: Canvas) {
        drawables.map {
            it.draw(canvas)
        }
    }

The DrawablePath’s draw(canvas) method calls drawPath(path, paint):

    override fun draw(canvas: Canvas) {
        canvas.drawPath(path, paint)
    }

The button to erase calls the following method in the view:

    fun erase() {
        extraCanvas.drawColor(Color.WHITE)
    }

The button to draw calls the following method in the view:

    fun draw() {
        doc.draw(extraCanvas)
        invalidate()
    }

When I do the draw -> click erase button -> click draw button, the Drawables in the list are not visible. However, I have validated that the DrawablePath’s draw method work. Also, if I do not draw in touchMove(), but call draw(), erase() and then draw() again in touchUp(), the lines are visible.

Anyone have an idea of how to achieve what I would like to do? Am I on the right path? Right now I have no clue what’s happening, and I really do not know how to debug this any further.

For the drawing to the canvas I followed the draw to Canvas objects codelab: https://developer.android.com/codelabs/advanced-android-kotlin-training-canvas#0 to enable the drawing.