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.
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 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.
I defined my activity to only be in portrait mode by :
android:screenOrientation="portrait"
When I take a picture with the camera function via intent, take this picture in landscape mode and turn the screen to portrait mode when saving it, i return to my activity again. What I dont understand is, that my activity for a short time is in landscape mode, is destroyed and then built again in portrait mode... as my onCreate ond onRestore functions need some time, the waiting time for the user is doubled...
Is there a workaround or something else?
You can also register your activity to explicitly handle any orientation changes by adding android:configChanges="orientation" to the activity definition and then overriding the onCofigurationChanged method in your activity like this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
I'm not sure if it would help with your specific problem, but I remember doing this when I wanted an activity to only display in portrait mode. Let me know if it helps :)
Add android:configChanges="orientation" to your <activity> tag. This way the activity will not be recreated on orientation change, but will just receive a callback onConfigurationChanged which you can ignore.
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.