Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix keyboard not hiding on RN 62+ #592

Merged
merged 1 commit into from
Aug 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 androidx.annotation.Nullable;
Expand Down Expand Up @@ -56,6 +58,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