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);
}
}
Related
If I multitask then when I leave my app the screen state is saved, and when I return to it the saved screen state is loaded. The problem is that only the screen state is loaded, the state variables in code are deleted, so there is a conflict between the screen state and application state.
Specific example: My app has two modes. In mode "A" the user can edit the size of an image, in mode "B" the user can edit the alpha of an image. The app starts in mode "A". Then the user switches to mode "B" and edits the alpha of the image. After this if the user leaves the app and then returns to it then the activity is automatically restarted and because of this the mode of the app becomes "A" (the default mode). But android applies the automatically saved alpha value set by the user in mode "B". In mode "A" there should be NEVER any alpha visible. If we are in mode "A" then I set
imgLeft.setAlpha(1f);
But this is overwritten by android with the saved screen state. So, how do I disable the automatic saving and loading of the screen state?
I want to write a custom screen state save and load. This was already a requested feature in my app, so there is no extra work from me. I just need android to stop interfering with my code.
It's usually not worth it to write a custom save/restore state if you don't have to.
It is probably better, and easier, to run the setAlpha(...) code in the activity lifecycle after the state is restored. That way it will stay default Android restore code, but still allow you to restore the parts of the state that your code manually changes.
Assuming this is in an Activity (as opposed to a Fragment which has a different lifecycle), you should move this from the onStart() to the onResume() part of the lifecycle.
#Override
protected void onResume() {
if (stateA) {
imgLeft.setAlpha(1f);
}
}
I would like to ask if the following behaviour is possible and, if it is, how I can achieve it. I need this behaviour for a very specific reason.
I have two activities, I will call them Activity A and Activity B. Now let's look at this example:
From activity A, in portrait orientation, I launch activity B (which has a fixed landscape orientation). The user, as the activity B is displayed in landscape, obviously rotates the device. The activity B is finished and activity A is resumed. The user keeps the device in landscape orientation so the orientation of the activity A will immediately be changed just after is resumed.
What I want is avoid that the orientation of the activity A changes when the user goes back to it. I want to resume the activity A in the last orientation it had and only allow orientation changes after that.
Locking the orientation is not an option as I want to allow the user to change it anytime.
The android:configChanges="orientation|screenSize" solution is neither an option as I want my activity to be re-created after an orientation change.
Do I have an option to achieve what I want?
Thanks!
EDIT: I forgot to say that saving the current orientation before the activity A is destroyed and restoring it when it is created again is not an option too. Activity A should NOT be re-created after activity B finishes.
EDIT 2: I was thinking now and I realized that I could rewrite the question from another point of view.
I would like to avoid the re-creation of the activity A when it is resumed after activity B finishes.
Afaik, when the device orientation change occurs, a message is sent to ActivityThread (with value RELAUNCH_ACTIVITY). That causes the activity A to be re-created with the new screen orientation.
I want to avoid the activity A relaunch as that would solve my problems and would keep the last orientation it had.
I don't think it is possible to do this as you've described. When the user returns from the landscape activity to your previous activity you would need to lock the orientation to portrait (in order to ignore the landscape orientation that the phone is now in). Now, if the user rotates the phone, you won't see the rotation and you won't be able to change the orientation. Since you can't really tell whether the user just rotated the phone or if the rotation is due to returning from a landscape-only activity, you can't tell these 2 conditions apart.
However, assuming that you can somehow differentiate between these 2 cases, try the following:
If you don't want Android to recreate your activity on an orientation change (even only in certain circumstances), then you will need to tell Android that you want to handle orientation changes by yourself by setting
android:configChanges="orientation|screenSize"
in the manifest.
Now you'll need to implement onConfigurationChanged() to get the behaviour that you want. In the case where your activity has launched another activity with fixed orientation, you'll want to save the current screen orientation before launching that activity. When the user returns to your activity Android won't destroy it and create a new instance it will just call onConfigurationChanged(). When you get the onConfigurationChanged() call you'll need to check if the user just rotated the device or if this is the result of returning from an orientation-locked activity. In the case where this is the return after launching another activity you can then lock the screen orientation to the previous orientation by calling setRequestedOrientation(). In the case where the user just rotated the phone, you can then either:
Recreate the activity by calling Activity.recreate() Note: this only works if you are running on API level 11 or higher
Recreate the activity by calling your own method that basically does everything necessary to recreate the Views (call setContentView()) and reset your member variables as if the activity were recreated
Recreate the activity by launching your activity again: startActivity(new Intent(this, this.class) and then calling finish() to get rid of the current instance.
You, probably could store your activity's current orientation in shared preferences in onStop() and restore it in onResume()
I have developed an application and it contains web view.
My issue
When my phone's orientation changes from portrait to landscape the whole application loads again and the web view reloads showing the first page of website.
So I am getting confused about the screen orientation or saving the data during that phase, so how do I fix it...
Yes, in Android the Activity is destroyed and recreated when your change the screen orientation. Thus you need to restore your applications state on recreation. On way is to use the callback method onSaveInstanceState() which you can use to save the state in a Bundle.
The Activities are explained here: http://developer.android.com/guide/components/activities.html and I suggest you take a look on that page for examples and more detailed instructions.
The default behaviour is to restart the activity when a configuration change happens (such as orientation change).
To override this you need to tell the system you'll handle orientation change yourself by adding this to your manifest file in your <activity> element:
android:configChanges="keyboardHidden|orientation|screenSize"
You may also want to override onConfigurationChanged which will be called when such a change happens.
See http://developer.android.com/guide/topics/manifest/activity-element.html#config
The Android Activity lifecycle clearly indicates this behaviour .
What happens
Whenever you start an Activty is gets created(after onStart() method) the
onCreate(Bundle onSavedInstance)
The variable onSavedInstance mentioned above initially recieves null as nothing is saved.
But during the orientation the whole layout hierarchy has to adjust according to the new mode from an existing mode(from portrait->landscape or vice-versa).This change may remove the previous data that you had in your activity.So to avoid such a problem(loosing data), there is a method
onConfigurationChange(Bundle saveSomething)
This method will be called for you to handle some configuration changes into this,
The default implementation will save you some data like some text in an editText.
Note This method as far the specs goes should be used to save some trivial data.
Example
Suppose you had applied a background colour to the activty layout and now you rotated it default implementation won't save it but onConfigurationChange if you want you can save it like this
saveSomething.putInt("color",1);
Inside onCreate
protected void onCreate(Bundle onSavedInstance){
if(onSavedInstance!=null){
int color=onSavedInstance.getInt("color");
if(color==1){
setBackgroundColor(Color.BLACK);
}
}
}
Add the following line inside the activity element of your manifest file you will be handling the changes in configuration
android:configChanges="keyboardHidden|orientation|screenSize"
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Flash light is getting switch off while changing the orientation
I am building an android app. On configuration change the flash light goes off. Then I need to again click the ON button to make it on. how can it remain switched on in my app?
Basic problem is that configuration change by default recreates Activity, so if you do some recurse management in activity you have to remember that.
There are two opposite solutions:
disable recreation of Activity on configuration change by adding in manifest property android:configChanges of Activity list of configuration changes which you will handle manually, for example "keyboardHidden|orientation|screenSize" (screenSize is needed since Adnroid 3.1). In this case you can handle configuration change by overriding method onConfigurationChanged.
handle recreation of activity by overriding method onSaveInstanceState and saving state of activity there inside of bundle. Then in onCreate when parameter savedInstanceState is set (not null) you should restore state from that bundle (restore state of flash light).
Choice of method depends of you activity design.
Add android:configChanges="keyboardHidden|orientation" to your activity in android manifest file.
When ever you are changing the orientation the full activity is been destroyed and a new activity is been created.So what happening is that the activity is loosing the current state of the flash and it turns off.
Try disabling the orientation changes in the manifest.And save the current state of the flsh in the Onstop and reset the state in the OnStart.
This will work for you I believe.
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