I start an activity with startActivityForResult() but after the orientatikn changed and i return to the onActivityResult() method the requestCode not the same. I would like to use different layout if the screen orientation is in landscape mode.
I tried in the manifest the configuration with orientation and screenSize params, but if i use this the layout not changed to the landscape layout.
I started the activity with requestCode=0 but after the orientation changed i see that the requestCode = 66357 or something.
you can declare that your activity handles the configuration change itself, which prevents the system from restarting your activity.
In your AndriodMainfest.xml define configChanges
<activity android:name=".ResultActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
Then in your ResultActivity.java(that you call to get result) define onConfigurationChanged()
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
official link
Create two types of the layout folders. "layout-land" and "layout-port". put the required xml in both the directory with same name.
You have two options:
A)Set your app to not rotate.
B)Or accept that the world is not perfect yet and modify your app to handle configuration change
Related
I am trying to handle change orientation. I enforce orientation in Manifest:
android:screenOrientation="landscape"
android:configChanges="orientation|screenSize"
Then in Activity I set onConfigurationChanged, but it doesn't work. It doesn't handle the change orientation. When I don't set orientation in Manifest, everything works perfect, but I need landscape when the activity starts.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
startActivity(
new Intent(GraphActivity.this, MainActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
}
}
Thanks guys
onConfigurationChanged won't be called if you have set fixed orientation in manifest
what if user runs app in portrait? it will start with horizontal layout, then if user still want to be in portrait then he must rotate device to landscape and back again to portrait? your described behavior looks to me like bad UX...
but, answering: there is no "landscape-only-on-start" for screenOrientation... if you really want to program such behavior then you have to use some code, especially setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_HORIZONTAL); method, but probably also some manual orientation calculation (sensor callbacks)
I have a ListFragment which opens an Activity when it is clicked. Now, my problem is that my listener is lost when the screen is rotated. The click events does not respond. I have tried android:configChanges , it fixes the listener problem but the layout of the whole activity looks weird. Any possible solutions to set listener again on configuration change?
Best solution: The better way to handle the problem by using onSaveInstanceState().see this link Handling Runtime Changes
No.2: You can detect the change and then handle it through the below method:
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
// Checks the orientation
if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape mode", Toast.LENGTH_SHORT).show();
} else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait mode", Toast.LENGTH_SHORT).show();
}
}
The forced solution: if you don't want to recycle the activity lifecycle on orientation change then you can add this in your manifest:
<activity android:name=".YourActivity"
android:configChanges="orientation|keyboardHidden"
android:label="#string/app_name">
The last one is the worst solution, but works.
As my suggestion never use the last one because you can't able to do any kind of recycle things in future. but for emergency solution its good.
First time I've ever tried this so do not know about it but need it fixed ASAP. Im trying to make it do a new intent when i rotate the device to landscape.
Here's my code:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(),
VideoLandscape.class);
i.putExtra("url", LINK);
i.putExtra("small", videoPath);
startActivity(i);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
//Manifest
<activity
android:name="com.****.media.tv.Video"
android:configChanges="orientation|keyboardHidden"
android:label="#string/title_TV">
</activity>
<activity
android:name="com.****.media.tv.VideoLandscape"
android:label="#string/title_TV">
</activity>
Many Thanks
Charlton Santana
android:configChanges="keyboardHidden|orientation|screenSize"
in activity tag of meanifeast
You need to add |screenSize attribute in android:configChanges attribute like given below
android:configChanges="keyboardHidden|orientation|screenSize"
Since there is a little change after API level 13 which is mentioned in the doc for handeling onConfigurationChanged()
Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).
From here
I'm trying to handle screen-rotation myself and I need to know: How do I tell my activity to use another .xml file for the new layout? I've tried different things but none of them worked.
Best regards
You do not handle it in code, rather, you put your other layout file (with the same name) in the appropriate folder (then Android does the rest).
Like this (pseudo-code):
layout_landscape\my_layout.xml
layout\my_layout.xml
Link:
http://developer.android.com/guide/practices/screens_support.html
try setContentView(your new xml), you should override onConfigurationChanged() and act accordingly
Orientation change usually calls onCreate() again, and restarts the activity at a new configuration.
make sure you do the following:
Set the Activity's manifest to intercept orientation change events
android:configChanges="keyboardHidden|orientation"
Override onConfigurationChanged()
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == android.content.res.Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.layout_portrait);
}
else if (newConfig.orientation == android.content.res.Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.layout_landscape);
}
}
And read this, it's a great document about faster orientation changing.
I realize that there are a couple other posts on this topic, however the solutions for those posts are not working for me.
Basically, I want to cease my Activity from restarting upon a device orientation change. To do this, I have modified the activity in the manifest file:
<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden"></activity>
and I have overridden onConfigurationChanged() in my Activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
System.out.println("IN onConfigurationChanged()");
}
However, the activity is still restarting upon an orientation change, and the onConfigurationChanged() method is not being called.
Does anyone know why this may be happening?
You should use 13 API and set this configuration in your activity's part of the manifest:
android:configChanges="orientation|keyboardHidden|screenSize"
It works fine. At all Android version.
Change your manifest to following
<activity android:name=".MyActivity" android:configChanges="orientation|keyboardHidden|screenSize"></activity>
and refer this link for detailed explanation orientation issue
The only thing that worked was using getLastNonConfigurationInstance(). http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance()
You should not use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); method call any where in your application this will avoid calling onConfigChanged() method.
if you define configchanges=orientation in your manifest then the activity won't restart but instead onConfigurationChanged will be call as you have currently have it implemented. First try to log that with the log class Log (this is the proper way to log things in android don't use System out for this its considered a bad practice) and before super but that is just a 1% chance it will fix what is happening to you.
The second case is that you have the current activity nested in a tabHost for example or Activity Group. if your activity has a parent activity then configuration change needs to be added in that one and the callback will happen there.
If that is the case and you want to forward the result or also do something in the child then you need to get a reference to the child in the parent and call a method on it for the changes.
if you have a fragment then you need this also:
void setRetainInstance(boolean retain)
Control whether a fragment
instance is retained across Activity re-creation (such as from a
configuration change).
i ran into this and setting it to 'true' fixed it.
I used this, and it helped:
package="com.s2dio.evallet"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
Modefiy your onConfigurationChanged method to the following
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}