Skip to content

Commit

Permalink
Restore selection
Browse files Browse the repository at this point in the history
  • Loading branch information
quthla committed Jul 23, 2024
1 parent e39e6a8 commit 2ee0e3a
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public void run() {
};
private TextWatcher textWatcher;
private boolean keyboardShown = false;
private int lastSelection = -1;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
Expand Down Expand Up @@ -151,6 +152,19 @@ public void afterTextChanged(final Editable s) {
}
}
};

if (savedInstanceState != null) {
lastSelection = savedInstanceState.getInt("lastSelection", -1);
}
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);

if (binding.editContent != null && binding.editContent.hasFocus()) {
outState.putInt("lastSelection", binding.editContent.getSelectionStart());
}
}

@Override
Expand Down Expand Up @@ -178,6 +192,10 @@ protected void onNoteLoaded(Note note) {
if (sp.getBoolean(getString(R.string.pref_key_font), false)) {
binding.editContent.setTypeface(Typeface.MONOSPACE);
}

if (lastSelection > 0 && binding.editContent.length() >= lastSelection) {
binding.editContent.setSelection(lastSelection);
}
}

private void openSoftKeyboard() {
Expand Down

0 comments on commit 2ee0e3a

Please sign in to comment.