Search code examples
androidandroid-tabactivity

Prevent softkeyboard appear from other activity


I got a EditText on my first tab. Of course, soft keyboard appearing when I touch on my EditText. But when I access to second tab, it means second tab activity, soft keyboard stay appearing on my first tab and don't disappear even though there is no EditText on my second tab activity.

I want to prevent softkeyboard from appearing when I access to my second tab.

How can I solve this problems?

Here's my manifest files

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.androidaccountbook"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.example.androidaccountbook.AccountTabActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden">
             <!--  screenOrientation prevent from rotation -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity android:name="com.example.androidaccountbook.accountMainThread"
             android:windowSoftInputMode="stateHidden" />

        <!-- activity for intent -->
        <activity android:name="com.example.androidaccountbook.TodayExpenseList"
            android:windowSoftInputMode="stateHidden" />        
        <activity android:name="com.example.androidaccountbook.settingThread" 
            android:windowSoftInputMode="stateHidden"/>
        <activity android:name="com.example.androidaccountbook.FailedConnectDatabase" 
            android:windowSoftInputMode="stateHidden"/>

    </application>

    <uses-permission android:name="android.permission.INTERNET"/>

</manifest>

Solution

  • I figured how to works on it and I tried it, it works.

    Because I want to access to second activity, and surely life-activity onPause method will occurs. So, I wrote the following hideKeyboard method on my onPause method.

    This is how onPause method looks like

    @Override
    protected void onPause(){
        super.onPause();
    
        hideKeyboard(this,editText);
    
    }
    

    This is my hideKeyboard method looks like

    public void hideKeyboard(Context context, EditText text){
            InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(text.getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
        }