Skip to content

Commit

Permalink
Tweaks to maintaining keyboard state
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Oct 7, 2023
1 parent 06d2c4a commit 0798067
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
android:label="@string/app_name"
android:launchMode="singleTop"
android:taskAffinity=".activity.MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize">
android:windowSoftInputMode="stateUnchanged|adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down Expand Up @@ -179,7 +179,7 @@
android:parentActivityName=".activity.MainActivity"
android:taskAffinity=".activity.DocumentActivity"
android:theme="@style/AppTheme.Unified.StartupFlash"
android:windowSoftInputMode="stateHidden|adjustResize"
android:windowSoftInputMode="stateUnchanged|adjustResize"
tools:targetApi="lollipop">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
Expand Down Expand Up @@ -99,7 +101,6 @@ public static DocumentEditAndViewFragment newInstance(final @NonNull Document do
private MarkorWebViewClient _webViewClient;
private boolean _nextConvertToPrintMode = false;
private MenuItem _saveMenuItem, _undoMenuItem, _redoMenuItem;
private Boolean defocusImeState = null;

public DocumentEditAndViewFragment() {
super();
Expand Down Expand Up @@ -227,14 +228,23 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
});
_hlEditor.addTextChangedListener(GsTextWatcherAdapter.after(s -> debounced.run()));

// Restore keyboard when we regain focus
// We set the keyboard to be hidden if it was hidden when we lost focus
// This works well to preserve keyboard state.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
final Window window = activity.getWindow();
final int adjustResize = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
final int unchanged = WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED | adjustResize;
final int hidden = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | adjustResize;

_hlEditor.getViewTreeObserver().addOnWindowFocusChangeListener(hasFocus -> {
if (hasFocus && defocusImeState != null) {
_cu.showSoftKeyboard(getActivity(), defocusImeState, _hlEditor);
defocusImeState = null;
if (hasFocus) {
// Restore old state
_hlEditor.postDelayed(() -> window.setSoftInputMode(unchanged), 500);
} else {
defocusImeState = TextViewUtils.isImeOpen(_hlEditor);
final Boolean isOpen = TextViewUtils.isImeOpen(_hlEditor);
if (isOpen != null && !isOpen) {
window.setSoftInputMode(hidden);
}
}
});
}
Expand Down

0 comments on commit 0798067

Please sign in to comment.