Skip to content

Commit

Permalink
Fix issue with soft keyboard not showing from time to time on Android (
Browse files Browse the repository at this point in the history
  • Loading branch information
kmagiera authored Sep 9, 2019
1 parent de9bfc3 commit 6508386
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
45 changes: 40 additions & 5 deletions android/src/main/java/com/swmansion/rnscreens/Screen.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.swmansion.rnscreens;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -63,6 +61,22 @@ public void onDestroy() {
}
}

private static OnAttachStateChangeListener sShowSoftKeyboardOnAttach = new OnAttachStateChangeListener() {

@Override
public void onViewAttachedToWindow(View view) {
InputMethodManager inputMethodManager =
(InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(view, 0);
view.removeOnAttachStateChangeListener(sShowSoftKeyboardOnAttach);
}

@Override
public void onViewDetachedFromWindow(View view) {

}
};

private final Fragment mFragment;
private final EventDispatcher mEventDispatcher;
private @Nullable ScreenContainer mContainer;
Expand All @@ -88,6 +102,27 @@ protected void onDetachedFromWindow() {
clearDisappearingChildren();
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
// This method implements a workaround for RN's autoFocus functionality. Because of the way
// autoFocus is implemented it sometimes gets triggered before native text view is mounted. As
// a result Android ignores calls for opening soft keyboard and here we trigger it manually
// again after the screen is attached.
View view = getFocusedChild();
if (view != null) {
while (view instanceof ViewGroup) {
view = ((ViewGroup) view).getFocusedChild();
}
if (view instanceof TextView) {
TextView textView = (TextView) view;
if (textView.getShowSoftInputOnFocus()) {
textView.addOnAttachStateChangeListener(sShowSoftKeyboardOnAttach);
}
}
}
}

/**
* While transitioning this property allows to optimize rendering behavior on Android and provide
* a correct blending options for the animated screen. It is turned on automatically by the container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import java.util.HashSet;
import java.util.Set;

import static com.swmansion.rnscreens.Screen.StackAnimation.*;

public class ScreenStack extends ScreenContainer {

private final ArrayList<Screen> mStack = new ArrayList<>();
Expand Down

0 comments on commit 6508386

Please sign in to comment.