Skip to content

Commit

Permalink
Fix keyboard not hiding on RN 62+ (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmagiera authored Aug 6, 2020
1 parent a0b0008 commit 49561e7
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions android/src/main/java/com/swmansion/rnscreens/ScreenFragment.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.swmansion.rnscreens;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;

import com.facebook.react.bridge.ReactContext;
Expand Down Expand Up @@ -60,6 +62,24 @@ public View onCreateView(LayoutInflater inflater,
return wrapper;
}

@Override
public void onDetach() {
super.onDetach();
// The below line is a workaround for an issue with keyboard handling within fragments. Despite
// Android handles input focus on the fragments that leave the screen, the keyboard stays open
// in a number of cases. The issue can be best reproduced on Android 5 devices, before some changes
// in Android's InputMethodManager have been introduced (specifically around dismissing the
// keyboard in onDetachedFromWindow). However, we also noticed the keyboard issue happen
// intermittently on recent versions of Android as well. The issue hasn't been previously noticed
// as in React Native <= 0.61 there was a logic that'd trigger keyboard dismiss upon a blur event
// (the blur even gets dispatched properly, the keyboard just stays open despite that) – note
// the change in RN core here: https://github.com/facebook/react-native/commit/e9b4928311513d3cbbd9d875827694eab6cfa932
// The workaround is to force-hide keyboard passing the fragment's view token when the given fragment
// is detached. It is safe to call this method regardless of whether the keyboard was open or not.
((InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(mScreenView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

public Screen getScreen() {
return mScreenView;
}
Expand Down

0 comments on commit 49561e7

Please sign in to comment.