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: