Stack Logic Problem....
Archived a year ago
.
GFTK
Verified
I'm making an image editor in Android Studio Java. I am making an undo and redo features by having an undo stack - which every change in the image is added onto, and a redo stack - every undo, the image is stored there. The problem I have is for some reason, for any amount of undos I do, the *last* redo will never work. It doesn't update the image.
For example: I change the image from black to white, then undo. If I try to redo, the image will stay white.
But, if I do black -> red -> white, then undo twice (back to black), and redo twice, it will change to red but not change to black after.
This is my code:
```java
public void undo(View view) {
if (!undoStack.isEmpty()) {
// Save the current state before popping
redoStack.push(bitmap);
// Restore the last saved state
bitmap = undoStack.pop();
img.setImageBitmap(bitmap);
}
}
public void redo(View view) {
if (!redoStack.isEmpty()) {
// Save the current state before popping
undoStack.push(bitmap);
// Restore the last undone state
bitmap = redoStack.pop();
img.setImageBitmap(bitmap);
}
}
```
