Skip to content

Commit

Permalink
Keep cursor position when switching away
Browse files Browse the repository at this point in the history
* Fixes #162
  • Loading branch information
gsantner committed Dec 28, 2017
1 parent 602256a commit 1b82400
Showing 1 changed file with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class DocumentEditFragment extends BaseFragment implements TextFormat.Tex
public static final int HISTORY_DELTA = 5000;
public static final String FRAGMENT_TAG = "DocumentEditFragment";
private static final String SAVESTATE_DOCUMENT = "DOCUMENT";
private static final String SAVESTATE_CURSOR_POS = "CURSOR_POS";
public static boolean showPreviewOnBack = false;

public static DocumentEditFragment newInstance(Document document) {
Expand Down Expand Up @@ -100,6 +101,12 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
}
_document = loadDocument();
loadDocumentIntoUi();
if (savedInstanceState != null && savedInstanceState.containsKey(SAVESTATE_CURSOR_POS)) {
int cursor = savedInstanceState.getInt(SAVESTATE_CURSOR_POS);
if (cursor >= 0 && cursor < _hlEditor.length()){
_hlEditor.setSelection(cursor);
}
}

new ActivityUtils(getActivity()).hideSoftKeyboard();
AppSettings appSettings = new AppSettings(_context);
Expand All @@ -111,7 +118,11 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
public void onResume() {
super.onResume();
checkReloadDisk();
int cursor = _hlEditor.getSelectionStart();
_hlEditor.setText(_document.getContent());
if (cursor >= 0 && cursor < _hlEditor.length()){
_hlEditor.setSelection(cursor);
}
}

@Override
Expand Down Expand Up @@ -271,6 +282,7 @@ public boolean saveDocument() {
public void onSaveInstanceState(@NonNull Bundle outState) {
saveDocument();
outState.putSerializable(SAVESTATE_DOCUMENT, _document);
outState.putSerializable(SAVESTATE_CURSOR_POS, _hlEditor.getSelectionStart());
super.onSaveInstanceState(outState);
}

Expand Down

0 comments on commit 1b82400

Please sign in to comment.