Draw beautiful diagram effortlessly on ipad. Click here to know more..

Sunday, April 1, 2012

How to implement UndoManager (with example in Java on Android)

Undo-redo is a must-to-have feature in editing programs. This blog post describes how to implement undo-redo in your program. The example taken here is in Java but the concept is very simple and can be implemented easily in any language. 
Here is idea to implement the undo-redo. First of all you need these: UndoManager - a class that implements undo-redo features. UserAction - an abstract base class for all user actions.
The operations in the UndoManager are: undo, redo, canUndo, canRedo, addAction And the operations in UserAction are: perform, undo
As said above, the UserAction abstracts user actions which user can undo. For example in text editing program, inserting a character at a particular cursor location is an action which user can undo. So you need to implement this abstract class for all type of user's undo-able actions. 
Lets start with some real code. The code here is taken from an Android app "Scratchpad" 
This is a simple drawing program using touch. User can draw on the screen using touch dragging on Android tablet screen. So the user action,in this case, is draw a stroke on canvas. A stroke is defined as the drawing done between users actions of starting touch drag and stopping touch drag. Here is UndoManager class:
import java.util.*;
public class UndoManager {
 private UndoManager(){
  undoActions = new Stack();
  redoActions = new Stack();
 public boolean canUndo(){
  return undoActions.size() > 0;
 public boolean canRedo(){
  return redoActions.size() > 0;
 public void undo(){
   UserAction act = this.undoActions.pop();
 public void redo(){
   UserAction act = this.redoActions.pop();
 public void add(UserAction action){
 private Stack undoActions;
 private Stack redoActions;

The code for UserAction
public abstract class UserAction {
 public abstract void perform();
 public abstract void undo();
Here is code for StrokeAction
public class StrokeAction extends UserAction{
 public StrokeAction(DrawingStroke ds){
  this.stroke = ds;
 public void perform(){
 public void undo(){
 private DrawingStroke stroke;

Whenever the user completes a drawing stroke in Scratchpad, an object of DrawingAction is created. The ObjectManager in the code above contains the all stroke objects. Whenever user performs a drawing, we add the stroke to object manager. The drawing of all available objects are managed ObjectManager. And when user wants to undo, we simply remove the stroke from object manager. For simplicity and readability, I am not adding the code of ObjectManager here. 

The code of UndoManager and UserAction is very generic and can be used any application. UserActions has to be implemented for for each undo-able user action type. This implementation will be application dependent.


  1. Hi, This is Jamuna from Chennai. I have read your blog and I got some knowledgeable information through this blog. Really useful blog. Keep update your blog.

    Java Training Chennai

  2. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

  3. Java Training Institutes Java Training Institutes
    Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

    Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai

  4. Thank You for sharing your article. I like it. It is great information about Java. We provides TIBCO Online Training in Hyderabad.

  5. Thanks for taking time to share this post.It is really useful.Continue sharing more like this.
    Regards, Java Training in Chennai

  6. I have completely read your post and the content is crisp and clear.Thank you for posting such an informative article, I have decided to follow your blog so that I can myself updated.
    Java Training in Chennai

  7. I completely read your blog.It is very useful to me .I have imporve my coading knowledge. Thanks for sharing. Keep sharing more blogs.

    Software Testing Training in chennai

  8. I found your blog while searching for the updates in Selenium WebDriver,I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about selenium tutorials for beginners with reference of your blog.

  9. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    Java Training in Bangalore

  10. I used to write critical essays about these platforms using useful info about writing critical analysis essays and I must say that I was surprised that these system have such high security level.