0

I'm using this code to hide the softkeyboard which I got from here: Close/hide the Android Soft Keyboard

public class HideKeyboardUtils {

    public static void hideKeyboard(Activity activity) {
        InputMethodManager inputMethodManager = (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);
        }
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}

In my Application class, each time my Application crashes, this function runs so that it will hide the keyboard on the application crashing:

    final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable throwable) {
            HideKeyboardUtils.hideKeyboard( // how do I get an instance of an activity here?);
        }
    });

I don't know how I can get an instance of the activity in my Application class and from reading this thread, it doesn't seem possible - Getting Main Activity from Main Application

I see I need the activity so that I can get a view from the activity in order to get the WindowToken. Is there a way I can get a view in the application class so that the keyboard will hide on the Application crashing?

See example of a crash below:

enter image description here

Community
  • 1
  • 1
Simon
  • 18,312
  • 22
  • 130
  • 197
  • Um, why are you worrying about the keyboard here? If you crashed, I would think that you have other things to worry about. Moreover, there may not *be* a valid activity instance for your app, depending on where you crashed and the state of the task at that time. – CommonsWare Apr 09 '16 at 20:59
  • The problem is the app will crash and the keyboard will be left behind as that is an android system built-in softkeyboard. The majority of the time, I will get back to the android home screen with the keyboard still visible after the app crashes. Or I would get the dialog box with the words "Sorry, your app has stopped working" and the "OK" is obscured by the keyboard. Of course I would worry about the actual code but this is more of a user experience concern. – Simon Apr 09 '16 at 21:01
  • Interesting. I don't recall seeing this, but I can't remember the last time I saw a crash with the keyboard exposed. However, my point still holds: if you're at the home screen, and the keyboard is visible, there is no activity for you to work with. – CommonsWare Apr 09 '16 at 21:03
  • OK. I have attached a screenshot of how the app will crash. Thanks anyway, I guess the user will just have to live with it. – Simon Apr 09 '16 at 21:08
  • Either that, or find some way to hide the IME that doesn't require an `Activity`. I am skeptical that there's an option for that. – CommonsWare Apr 09 '16 at 21:10

0 Answers0