Going back in workflow

I have a need to go back to a previous stage in the workflow upon choose to go back and the subsequent task should be rest. if there are any completed tasks that should also get rest.
eg in the below WF the process is already at last stage and task A is completed while B is not if I get an amendment request and the workflow is pointed back to step 2 then I get Task A and task B twice after finishing step 2. how to rest the flow after redirecting it to step 2 from amendment taskWF

        .moveActivityIdTo(task.getTaskDefinitionKey(), historicTaskInstance.getTaskDefinitionKey())

You can use this function.


i have tried your code in rest api project but not sure about the state has been change or not please guide me how can i test that my current task has been change or not.

Task currentTask = taskService.createTaskQuery().taskId(currentTaskId).singleResult();

HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).orderByHistoricTaskInstanceEndTime().desc().list().get(0);

runtimeService.createChangeActivityStateBuilder().processInstanceId(currentTask.getProcessInstanceId()).moveActivityIdTo(currentTask.getTaskDefinitionKey(), historicTaskInstance.getTaskDefinitionKey());

Please guide me.