I am currently working on a multi-threaded game application for the Android platform... so far so good... I just got over a bug in my application which caused it to restart on orientation change (fixed by designating a specific orientation depending on the availability of a hardware keyboard or not, which is important 'cuz it is an online game with chat capabilities), and that works... BUT now i am trying to avoid the same problem when the user simply slides open the hardware keyboard. I'm not quite sure how to go about avoiding the restart of my application or the saving of the state of my application. Any solutions/suggestions?
In your <activity> tag in your manifest:
android:configChanges="orientation|keyboardHidden"
In your activity class:
#Override
public void onConfigurationChanged(final Configuration newConfig)
{
// Ignore orientation change to keep activity from restarting
super.onConfigurationChanged(newConfig);
}
Related
I have implemented facebook into my application.
Login process is working properly. When I click on facebook image then it opens facebook login window.
But When I rotate the emulator then it Close the login window.
Any solution.
Add this in manifest file for your Activity
android:configChanges="keyboardHidden|screenSize|orientation"
The approach I took was to not allow the OS to restart your activity after the layout configuration change. To accomplish this, add this line within activities that you want to prevent from restarting in your manifest file:
<activity
android:configChanges="orientation|keyboard"
...
>
Optionally, you can handle the configuration change in code in case there are some layout changes you want to make manually, such as reloading a new view from XML. This is done by overwriting the onConfigurationChanged() method in your Activity class:
#Override
public void onConfigurationChanged(Configuration newConfig)
{
//Handle config changes here, paying attention to
//the newConfig.orientation value
super.onConfigurationChanged(newConfig);
}
Problem may be because of screen orientation configuration change, you can try one of following solutions:
As some of the answers suggested, you could distinguish the cases of
your activity being created for the first time and being restored
from savedInstanceState. This is done by overriding
onSaveInstanceState and checking the parameter of onCreate.
You could lock the activity in one orientation by adding
android:screenOrientation="portrait" (or "landscape") to
in your manifest.
You could tell the system that you meant to handle screen changes
for yourself by specifying android:configChanges="screenOrientation"
in the tag. This way the activity will not be recreated,
but will receive a callback instead (which you can ignore as it's
not useful for you).
I'm developing an app that only some pages can be displayed on landscape orientation, I do not want to lock the screen on display, I wish to deny Android do a orientation change.
There's something that I can use for it? Like, at runtime on orientation change, Android asks if it's possible to go to that orientation?
If there's not, how can I lock the current screen orientation? I know that setRequestedOrientation can do that, but it implies in discover the current orientation (and even discover the correct orientation like 'normal' or 'inverse' portraits/landscapes) and lock to it.
EDIT:
There's only ONE activity on this app, and based on it's state (that changes trough time and user input) it can be rotated or not, that is my problem.
As seen at http://developer.android.com/guide/topics/resources/runtime-changes.html about configuration changes:
"Handle the configuration change yourself
Prevent the system from restarting your activity during certain configuration changes, but receive a callback when the configurations do change, so that you can manually update your activity as necessary."
Even with that the configuration changes, I wish to block this behavior before that.
Add one othese to each activity.
public void checkActivityPos() {
if(somthing == "this") {
myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}else {
myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
I have Acivity which in it's onCreate method makes requset in Asynctask to remote server, takes data and renders them. It's ok, but when I'm trying to change mode to landscape - hall this process starts from the very begining and I have to wait untill data come from server one more time. I don't need it. I need just renderng the same data to landscape mode. How to do it?
hall this process starts from the very begining
It is happening because your activity is restarting after orientation.
So to prevent restart do following.
Make following changes in your activity in manifest.
<activity android:name=".ActivityName"
android:configChanges="orientation|keyboardHidden|screensize" />
ScreenSize attribute seems to be added in 4.0 so don't mentioned it if you are running below 4.0.
And then add the following method to Your Activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
It will prevent your activity from restarting.
I am new to android application.
My Android application works well on emulator but running on real android device (Softbank 003 SH), when I rotate the device from portrait to landscape, the application stop unexpectedly.
Do you have any hint to solve this problem?
when rotate the device from landscape to portrait will re-create the activity so thread will stop and if any builder is running will cause a error so :
Start by adding the
android:configChanges node to your Activity's manifest node
android:configChanges="keyboardHidden|orientation"
Then within the Activity override the onConfigurationChanged method and call setContentView to force the GUI layout to be re-done in the new orientation.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.myLayout);
}
You can simulate rotation on the emulator with CTRL+F12.
As for the "unexpected" stop, it is because when you rotate, the Activity is reloaded (onCreate is run again, etc), and you probably didn't follow the Android way of coding and some of your variables end up uninitialized! It is a very common error to assume Android works like Windows (has applications), but very generally, it works more like the iPhone or dynamic webpages (has semi-independent forms).
Check the LogCat for the error.
I'm using MonoDroid but an equivalent Java answer can still help.
I'm using a portrait layout and a landscape layout so, if possible, I want to use the Android screen orientation to automatically destory/create activities.
My app is using TextToSpeech so in the activity's OnPause() I am stopping it, which works well when the Home key is pressed or an incoming call is happening. However I don't want to stop the TextToSpeech on a screen orientation change from the user.
Is there a simple way of detecting this change so that TextToSpeech isn't interrupted?
My activity's OnStop() code:
protected override void OnPause()
{
// I need this for Home key, intercepting phone calls, etc.
// But how can I prevent this for a screen orientation change?
// Need to enable screen orientation to get my portrait/landscape views
if(Text2Speech.IsTextToSpeechInitialised && Text2Speech.TextToSpeech != null)
Text2Speech.TextToSpeech.Stop();
base.OnPause();
}
As others have said in the comments on the question, I think you'll want to handle configuration changes yourself here. There are other configuration changes that can cause your activity to be restarted as well, such as revealing the device's hardware keyboard.
In Mono for Android you can specify which of these you want to handle yourself in the ActivityAttribute:
[Activity(ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden)]
public class MainActivity : Activity