简体   繁体   中英

Soft keyboard keeps opening up between fragments

Every time I navigate to a new fragment in my app the soft keyboard shows up. What I currently do: I have Fragment A that opens Fragment B, I put this in Fragment A:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    AndroidUtils.closeKeyboard(activity); //custom method I use to close the keyboard
}

And in Fragment B:

@Override
public void onStop() {
    AndroidUtils.closeKeyboard(activity);
    super.onStop();
}

I have had the best success doing this, but the problem is the keyboard will open during the transition then close when Fragment B has opened. I want the keyboard to appear not at all - and none of these fragments have EditTexts so I don't think it's a focus issue. This is what I have tried:

  1. Putting this in AndroidManifest.xml:

    android:windowSoftInputMode="stateHidden"

  2. Adding this to OnCreateView() of every fragment and even OnCreate():

    activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

This is a very weird bug, any ideas?

EDIT: My AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="App.App.AppName">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-sdk android:minSdkVersion="16" />

<application
    android:name=".Global"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:windowSoftInputMode="stateHidden">
    <activity
        android:name=".AppName.MainActivity"
        android:configChanges="orientation"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".firebase.DefaultFirebaseMessagingService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

</application>

public void setHideSoftKeyboard(View view) {
        InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

Call this method when your are opening Fragment B from Fragment A and pass any view to this method so it can close the keyboard.

I faced the exact same bug so here is the solution for anyone facing this in the future, If in your fragment you have a edittext as a child of another layout (Linear, constraint etc) add the following property to that layout

android:focusableInTouchMode="true" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM