简体   繁体   English

Android应用程序 - 每当蓝牙键盘连接/断开连接时触发onCreate

[英]Android App - Fires onCreate whenever bluetooth keyboard connects / disconnects

I have a fairly simple android app, which uses a bluetooth barcode scanner connected to it. 我有一个相当简单的Android应用程序,它使用连接到它的蓝牙条码扫描仪。

The problem I'm having, is that whenever the bluetooth scanner connects or disconnects from the android device (as it does when it goes to sleep/wakes up), it causes the onCreate method of the active window/layout to be called. 我遇到的问题是,每当蓝牙扫描器连接或断开与Android设备的连接时(就像它进入休眠/唤醒时一样),它会导致调用活动窗口/布局的onCreate方法。

Is there some way I can stop this? 有什么方法可以阻止这个吗?

Sounds like your activity is being restarted, because the connection of the scanner (which appears to your Android device as a keyboard) is considered a configuration change. 听起来您的活动正在重新启动,因为扫描仪的连接(在Android设备上显示为键盘)被视为配置更改。

I would strongly recommend that you modify your app so that any data is properly cached and restored when your activities are restarted - just like when the screen is rotated. 强烈建议您修改应用程序,以便在重新启动活动时正确缓存和恢复任何数据 - 就像旋转屏幕时一样。 Then connecting and disconnecting the scanner will not impact your users. 然后连接和断开扫描仪不会影响您的用户。

Alternatively, you can tell Android that your app will handle "keyboard" configuration changes and it will not restart your activities when the keyboard connects/disconnects. 或者,您可以告诉Android您的应用将处理“键盘”配置更改,并且在键盘连接/断开连接时不会重新启动您的活动。 However, it will still restart your activities for all the other configuration changes that you do not handle - and the list of configuration changes is kind of long. 但是,对于您未处理的所有其他配置更改,它仍将重新启动您的活动 - 并且配置更改列表有点长。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM