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?
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.
For me, adding "orientation|keyboard|keyboardHidden" to configChanges would still call onDestroy and onCreate of the activity. After a long research I found this article:
https://developer.samsung.com/sdp/blog/en-us/2017/12/07/samsung-dex-lifecycle-on-switching-between-mobile-and-samsung-dex-mode
Basically, if you use Lenovo's productivity mode or Samsung DeX, it would recreate the app nonetheless, so it's necessary to add all of these if you want for the app to not recreate itself when you add a physical keyboard.:
<activity android:configChanges="orientation|screenSize|screenLayout|smallestScreenSize|uiMode|density">
Also, add this for Samsung DeX inside your AndroidManifest:
<meta-data
android:name="com.samsung.android.keepalive.density"
android:value="true" />
Related
I'm running Android 7.1 and I cannot for the life of me figure out how to ensure "car mode" doesn't start when the device (honeywell CT60) is docked. I've already tried the following:
installing app "no dock"
installing app "car mode off"
getting app devs to add uiMode to the <ACITIVTY> element in the manifest.xml file.
None of these seem to work.
Does anyone have any advice or ideas?
Perhaps even an app that force the device to not go into car mode.
You're on the right track. According to Honeywell the app's AndroidManifest.xml has to be modified in the following way:
<manifest>
<application>
<activity android:configChanges="uiMode">
[...]
</activity>
</application>
</manifest>
This has to be added for each activity, where CAR_MODE should be disabled. It signals to the OS that the app will handle changes regarding the UiMode itself. Now instead of restarting the app to apply changes, the OS will leave handling this change up to the app.
If you want to find out how exactly it works, there already is a post explaining it. Alternatively you can find a guide about this topic at the Android Developer Docs.
I'm trying to create an app in Android, using Eclipse ADT and I use a HTC phone. Here's the problem: after running the app on the phone from Eclipse, I add elements to my ListView and everything is fine, BUT when I turn the device in landscape mode the hole app seems like it restart, there's no more records inside. It's like new. Any ideas why and how I can solve this problem? Please don't tell me to deactivate the screen rotate option from phone settings.
This is the way android handles orientation changes. It reloads your whole activity. The normal way to handle this situation is to save the state of your activity in onPause() and then retrieve it back in onCreate().
Here is more information on the android activity lifecycle:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
EDIT:
You should implement onPause() anyway, because it will also be called if the phone rings in the middle of running your activity. In this case, when the user comes back from the call your activity will possibly be reloaded from scratch again and the user will lose their state.
There is a similar question here.
Basically you application is restarted. You can either force your application to portrait or follow the steps here to find out how to handle it properly.
I made answer over here about this sort of thing.
Please don't tell me to deactivate the screen rotate option from phone settings.
I'm not sure if you meant the following, but here's a solution:
Add android:configChanges="orientation|screenSize" to your <activity tag, which is in your AndroidManifest.xml, like so:
<activity
android:name="activity_name"
android:label="#string/app_name"
android:configChanges="orientation|screenSize" />
This will prevent the activity from being destroyed when the orientation is changed, like it usually would. There are other ways to fix this as well. Please leave a comment if this is not your desired solution. I can make up another.
Hi i am developing android application in tablet version. Here i am facing problem with orientation so i want to make my application in portrait mode only. For that i Used attribute like below snippet for each and every activity in my application.
android:configChanges="keyboard|orientation|keyboardHidden"
android:screenOrientation="portrait"
Application is running perfectly while testing in portrait mode. But it is not good while testing application with tablet physical position with landscape. It is first going to landscape then immediately going to portrait.
I used programmatically making the activity in portrait but no effect. Please provide me any help.
Here is my programmatic orientation fix
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
I tested in Motorolo device
Please suggest me to avoid my application going to landscape.
Try this...
<activity
android:label="#string/app_name"
android:name="abc.xyz.dd.MainScreen"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation">
By adding this in manifest file it will prevent from changing to landscape mode.
Finally i find the reason why orientation changes are occurring in android tablet. In ICS (version 4.0+) the tablet have the option like "Don't keep activities" in settings>> Developer tools>>
If the user checked that option the paused activity will killed and created when it is call again. what i mean the activity won't go for onPause() state it will go for onDestroy() and it will created again when press on back key. So the orientation behavior is different.
I did not get the idea how to handle it from our android code
I'm done with developing an android application and in phase of testing it. I've tested it with the simulator and everything is working perfect, but when I deploy it on devices, some of them when you change the screen orientation the application screen starts to flicker.
Any clue where to start the problem investigation from?
When orientation changes from portrait to landscape or vice versa, onCreate gets called again, making the application to start from the beginning. Make sure you are handling it in manifest by declaring android:configChanges="keyboardHidden|orientation" infront of your class name or override onConfigChanged() function.
Please check Both Device version and Developing Application Versions are the same or not, and clear the application after install to the device and then run.
I have developed a project which should run only in landscape mode. I set in manifest file like this android:screenOrientation="landscape". This is working fine in emulator but when i am running in nexus one it is still coming in portrait mode.Any idea any one?
Dunno, AndroidMirza. I tried it on two Nexus One phones, one running Gingerbread and one running Eclair, and it works as expected. Here's what I did:
Create Android project
Open AndroidManifest.xml
In <activity> tag, add:
android:screenOrientation="landscape"
In both cases (and AVD with GB), the screen orientation immediately locks to landscape in my Activity. I used [Ctrl] + [Space] to let the IDE auto-complete for me, to make sure there were no typos.
All I can think is, either there is an error in how you are specifying landscape, or something elsewhere in your code or manifest is defeating it.
When I want one of my apps running in landscape mode I add on my onCreate the following line :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Hope that is what you expect.