When ever i change in to another orientation the Activity onCreate() method is called again?
how ever i used
android:configChanges="orientation|keyboardHidden"
but it prevent to change another orientation.how to block on calling the OnCreate() again?
Thanks in advance...
Whenever the orientation changes, the application starts over in the onResume() method.
If you do not want it to change orientation when the user flips the phone, you need to set this in your manifest file:
<activity android:name="YourActivity"
android:screenOrientation="portrait" />
Or landscape if you wish.
If you do want your application to continue where it left of before the user changed orientation on the phone and you do want your application to change orientation too, you need to keep track of the state of the application and set these values in your onResume()-method.
This is a little dirty, but you could simply set a flag-style boolean for the activity. First time through, set the "already ran onCreate" flag to true and use that to avoid running the code multiple times.
Not very proper, but should work.
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 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"
I can perfectly manage orientation change programatically, by using setRequestedOrientation in onCreate.
Everything works really fine, until I plug a HDMI cable. After this, the tablet "wants" to be in landscape mode. So when I open an activity, it shows first in "landscape" and then right after it shows in "portrait", (because I'm calling setRequestedOrientation(variable_with_orientation_desired_by_the_user) where variable=ActivityInfo.SCREEN_ORIENTATION_PORTRAIT in onCreate As you can "see", my app keeps rotating on every activity the user opens;
If I set the orientantion for activity in the manifest, this problem is partially gone, since the orientation in manifest is the same that the user has picked up. If they are different, the same behavior starts again.
I tried this, but with no success:
#Override
public void onCreate(final Bundle saved)
{
setRequestedOrientation(ScreenOrientation);
super.onCreate(icicle);
}
So, is there any way to tell to android what orientantion it must create my activity, before it does create it? (But it cannot be in manifest)
I solved it by setting screenOrientation to "behind" in manifest file:
android:screenOrientation="behind"
I solved it using this declaration in manifest:
android:screenOrientation="locked"
on every activity declared in manifest with this issue.
And keep using setRequestedOrientation() programatically to define if landscape or portrait orientation within onCreate() method,
Hope it helps!
I have activity with:
android:screenOrientation="portrait"
and code:
if(blah blah blah)
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
in activity onCreate.
1. When test on smartphone "setRequestedOrientation" causes destroy activity and create again with desired screen orientation.
2. When test on tablet device this code runs only once activity. After "setRequestedOrientation" it just continues without destroy and create again activity. This happens when start code without debuger or without breakpoint in onCreate. If I stop in onCreate function - I see in logs - destroying and onCreate again.
How to catch specific situation with tablet?
UPDATE:
- Problem with tablet occurs when 'Orientation lock' if off. If 'Orientation lock' is enabled program works in same way as in smartphone.
UPDATE2:
- Is it possible to set preffered screen orientation to whole application. So when activity starts to know desired orientation. This will make unnessesary call 'setScreenOrientation' and avoid second activity creation.
After whole day spent with this issue I made following changes in my application:
1. Every activity /without first/ are declared as android:screenOrientation="behind".
2. In first activity I set preferred screen orientation using user selected mode /from my menu.
3. I call setRequestedOrientation only when user changed mode in my menu options.
This causes activities to be created only once. Finaly :)
No.. you cannot set preffered screen orientation for whole application... instead you should call for setScreenOrientation in respective activies only... Refere this link here...
You can set
<activity ActivityName = "test_activity"
android:configChanges="orientation">
</activity>
in your android manifest file. It will not allow activity to restart on orientation change but onconfigurationchange will be called normaly.
Later you can getdisplayorientation in onconfigurationchange and set whatever you want.
There is an application to load UI dynamically for a unique LinearLayout when button press.
The main UI is extends by Activity and others are extends by LinearLayout.The unique LinearLayout is in main UI.
Dynamically loading part is working smoothly.But when changing orientation of device or emulator it goes to main ui.I have a layout Stack to get current Layout(UI) at any given time.
I want to stop move to main UI when rotating the device/emulator.
Is it possible?
Thanks in advance.
It is probably because the activity is getting recreated on orientation change.
In your manifest file against the main activity add the following properties
<activity android:name=".YourMainActivity" android:configChanges="keyboardHidden|orientation"></activity>
I believew this happens because you don't correctly handle the event of the orientation change. You should override public void onConfigurationChanged (Configuration newConfig)method to handle this.
If you don't override it and don't force your actiivty to keep the orientation, when the orientation changes, the system will destroy your app and re-launch it. That's why you see the main UI again.
Solutions:
explicitly declare the orientation you want in the AndroidManifest.xml file, so such event will be ignored. something like: android:screenOrientation="portrait". See here for more choice.
override the onConfigurationChanged method and chage your UI as you want inside that method, or simply do nothing so that this event would be ignored.