-2

I am sharing some text to other apps using android default sharing

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

The Issue:

When I share to another app i.e (WhatsApp, slack, etc) open the keyboard in that app (i.e. to edit the message, etc.), and then send or return back to my application. in some cases the keyboard remains open in that app and when I try to close the keyboard on resume using the following code it is not working.

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();
}

And calling the function from onResume() of the fragment

if(getActivity()!=null){
    Utils.hideKeyBoard(getActivity(),getActivity().getCurrentFocus());
}

I have found many answers related to hiding the keyboard but any answer is not working in this case.

Note: in the normal case, when I use the hideKeyBoard() method, it is working perfectly. It is just this case that is not working. Can anyone help me?

Edit As I've mentioned above. that why this quesiton is not like the prviosly answered ones is explained in the above note. So kindle Plesase read that. And I've also tried this link. but not working.

Riddhi
  • 2,522
  • 5
  • 31
  • 52
  • What if you try to close keyboard from fragment host activity ? – M D May 17 '19 at 05:56
  • 3
    Have you tried *getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);* in _OnResume()_? – Piyush May 17 '19 at 05:58
  • @MD not working. :( – Riddhi May 17 '19 at 06:03
  • 1
    You can add `android:windowSoftInputMode="stateAlwaysHidden"` in menifest if you do not need a open keyboard on Activity startup . – ADM May 17 '19 at 06:07
  • @sanjeev the question is different. please read my note. that this in normal case the hiding keyboard works fine. and I've already tried the question in your comment before. but that was not working – Riddhi May 17 '19 at 07:07
  • @Riddhi https://stackoverflow.com/a/17789187/6514945 `` Check https://stackoverflow.com/a/2059394/6514945 – sanjeev May 17 '19 at 07:12
  • I've tried that already. but it was not working. because it was the fragment specific. – Riddhi May 17 '19 at 08:58
  • @Riddhi It's okay !! It was not duplicate question. Don't worry – Piyush May 17 '19 at 09:20
  • @Piyush How is your answer any diff from https://stackoverflow.com/a/2059394/6514945 bro?? She just had to try that out.. Anyway glad the problem is solved.. peace! – sanjeev May 17 '19 at 09:47

1 Answers1

0

You can try below code that may help you.

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);
    }

Here is some bonus

create activity instance instead use getActivity() methos on fragment. getActivity() returns null when fragment not visible

on your fragment host activity

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;
}

inside onResume() call hideKeyboard(MainActivity.getInstance())

Also, don't forget to add android:windowSoftInputMode="stateAlwaysHidden" in your Manifest

Mehul Solanki
  • 948
  • 8
  • 15
  • Please check out [this](https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard). that may help you. – Mehul Solanki May 17 '19 at 06:10
  • 2
    Instead of creating instance of activity in `onAttach()` method with reference of content use activity context in fragment. – Piyush May 17 '19 at 06:47
  • @Piyush This is a good approach. but this also returns `null` when fragment not visible and something inside `Handler` needs to call. like some local notification. at that time `getInstance()` is the only way to avoiding `nullPointerException`. but thanks to pointing it out. – Mehul Solanki May 17 '19 at 06:54
  • No. when fragment starts _onAttach()_ call first. So there is no way to any exception. – Piyush May 17 '19 at 07:00
  • 1
    I agree with @Piyush. and by passing the instance it can also cause memory leak too. – Riddhi May 17 '19 at 07:09