Java 从另一个应用程序返回共享时如何隐藏键盘onResume

Java 从另一个应用程序返回共享时如何隐藏键盘onResume,java,android,android-softkeyboard,Java,Android,Android Softkeyboard,我正在使用android默认共享功能向其他应用程序共享一些文本 Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_TEXT, sharingString); shareIntent.setType("text/plain"); startActivity(shareIntent); 问题: 当我共享到另一个应用程序(如Wh

我正在使用android默认共享功能向其他应用程序共享一些文本

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, sharingString);
shareIntent.setType("text/plain");
startActivity(shareIntent);
问题:

当我共享到另一个应用程序(如WhatsApp、slack等)时,打开该应用程序中的键盘(如编辑消息等),然后发送或返回到我的应用程序。在某些情况下,该应用程序中的键盘保持打开状态,当我尝试使用以下代码在resume上关闭键盘时,键盘不起作用

public static void hideKeyBoard(Activity activity, View view) {

    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);

    if (view == null) {
        view = new View(activity);
    }
    if (imm != null) {

        if(view.getWindowToken()!=null)
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

        if(view.getApplicationWindowToken()!=null)
            imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);

    }
    view.clearFocus();
}
并从片段的
onResume()
调用函数

if(getActivity()!=null){
    Utils.hideKeyBoard(getActivity(),getActivity().getCurrentFocus());
}
我已经找到了许多关于隐藏键盘的答案,但在这种情况下,任何答案都不起作用

注意:在正常情况下,当我使用
hideKeyBoard()
方法时,它工作正常。只是这个案子不起作用。有人能帮我吗

编辑
正如我上面提到的。上面的注释解释了为什么这个问题与先前回答的问题不同。所以kindle请阅读。我也试过链接。但不起作用。

您可以尝试下面的代码,这些代码可能会对您有所帮助

public static void hideKeyBoard(Activity activity, View view) {

    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);

    if (view == null) {
        view = new View(activity);
    }
    if (imm != null) {

        if(view.getWindowToken()!=null)
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

        if(view.getApplicationWindowToken()!=null)
            imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);

    }
    view.clearFocus();
}
public static void hideKeyboard(Activity activity) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        //Find the currently focused view, so we can grab the correct window token from it.
        View view = activity.getCurrentFocus();
        //If no view currently has focus, create a new one, just so we can grab a window token from it
        if (view == null) {
            view = new View(activity);
        }
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
这里有一些奖金

在片段上使用
getActivity()
methodos创建活动实例<当片段不可见时,code>getActivity()返回
null

关于您的片段宿主活动

public static MainActivity mainActivity;
public static MainActivity getInstance() {
    return mainActivity;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainActivity = this;
}
内部
onResume()
调用
hideKeyboard(MainActivity.getInstance())


另外,不要忘记在您的
清单中添加
android:windowSoftInputMode=“stateAllwayshidden”
,如果您试图从碎片主机活动中关闭键盘会怎么样?您是否尝试过getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT\u输入状态总是隐藏);在OnResume()中?@MD不工作:(您可以添加android:WindowsOfInputMode=“StateAllwaysHidden”
在menifest中,如果您在活动启动时不需要打开键盘。@sanjeev问题不同。请阅读我的说明。在正常情况下,隐藏键盘可以正常工作。我在您的评论中已经尝试过这个问题。但这不起作用。请签出。这可能会对您有所帮助。而不是创建activi实例引用内容的
onAttach()
方法中的ty在片段中使用活动上下文。@Piyush这是一个很好的方法。但是当片段不可见并且需要调用
处理程序中的某些内容时,这也会返回
null
。就像一些本地通知一样。此时
getInstance()
是避免
nullPointerException
的唯一方法。但要感谢指出它。不。当fragment首先启动onAttach()调用时。因此没有任何异常。我同意@Piyus。通过传递实例,它也可能导致内存泄漏。