From 5b7205b29912dc6c03a80a404af5b77d8eae4372 Mon Sep 17 00:00:00 2001 From: serso Date: Thu, 10 Mar 2016 14:20:43 +0100 Subject: [PATCH] Don't lose cursor position on device rotation --- .../java/org/solovyev/android/calculator/EditorView.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/EditorView.java b/app/src/main/java/org/solovyev/android/calculator/EditorView.java index 1e5a4400..685eabef 100644 --- a/app/src/main/java/org/solovyev/android/calculator/EditorView.java +++ b/app/src/main/java/org/solovyev/android/calculator/EditorView.java @@ -79,10 +79,14 @@ public class EditorView extends EditTextCompat { if (this.editor == editor) { return; } - this.editor = editor; if (editor != null) { + // avoid losing cursor position on focus restore. First request focus, then set cursor + // position. Consequent requestFocus() should be no-op + requestFocus(); setState(editor.getState()); } + // update editor at the end to avoid side-effects of #requestFocus() and #setState() + this.editor = editor; } @Override