Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android crashes on keyboard plugging

I'm developing an Android application in which I need support a USB keyboard. I haven't developed something to explicitly support the USB keyboard because I thought that every application can use a USB keyboard.

The problem is that when I'm trying to connect or disconnect a USB keyboard, my application crashes without an error trace from the application.

My device is a Lenovo with Android 4.1 installed. What can I do to resolve this problem?

like image 333
Bilias7 Avatar asked Feb 15 '23 11:02

Bilias7


1 Answers

An Activity configuration change occurs when the external keyboard is attached.

To resolve the crash, add the following element to the AndroidManifest.xml for all activities:

android:configChanges="orientation|keyboard|keyboardHidden"

Per the documentation, android:configChanges:

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

like image 140
Kapil Vij Avatar answered Feb 20 '23 09:02

Kapil Vij