I need help with the ActionBarSherlock when the orientation is changed the activity restart and disconnect all my services and the app crash i tried to add this lines to the file AdroidManifest.xml
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
and this lines in the activity file:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
but don't work.
any suggestion ???
Thank you and regards.
P.D.: Sorry for my bad english.
In Your manifest.xml add this line in your activity
<activity android:name="Your Activity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="adjustPan"/>
Related
I am now working on an android application, that is just gonna be for demo purposes. So during the development of the project, we didn't focus a lot on the orientation changes to the application. Whenever there is an orientation change, the application crashes. It is now too late to maintain the data for each activity or fragment.
So my question is, is there a way to set the orientation to potrait throughout the application. Even if the user rotates the phone, can we maintain the state to be potrait?
TL;TR: add this to your AndroidManifest.xml
<activity android:name=".YourActivity"
android:configChanges="orientation"
android:screenOrientation="portrait"/>
You can checkout this answer as well.
From Java:
#Override
public void onCreate(Bundle
savedInstanceState){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
From XML:
<activity
android:name=".ActivityName"
android:label="#string/app_name"
android:screenOrientation="portrait" />
THe manaifest.xml of activity as below:
<activity
android:name="com.wd.wuc.SearchStatusActivity"
android:label="#string/app_name"
android:screenOrientation="sensor"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="orientation|keyboard" >
</activity>
The Activity's code as below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_status);
System.out.println("onCreate!!");
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
System.out.println("onConfigurationChanged!!");
}
While I change orientation, I want to run onConfigurationChanged.
But the code only run onCreate method, but not onConfigurationChanged.
How can I modify it?
In the documentation (http://developer.android.com/guide/topics/manifest/activity-element.html), you can find:
If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.
You should try to add "|screenSize" to configChanges if you are targeting API 13 or higher:
android:configChanges="orientation|keyboard|screenSize"
android:configChanges="orientation|keyboard"
this line tells Dalvik compiler that you are handling changes of keyboard and Orientation.so kindly remove this line it will work for sure.
Replace
android:configChanges="orientation|keyboard"
with
android:configChanges="orientation|keyboardHidden"
in your manifest file.
Look this for your reference.
remove this from manifest file this tag. then it will work
android:configChanges="orientation|keyboard"
I developed on application using android sdk 4.0 and I install that .apk file in my samsung tab. When I run that application it is working properly. If I change the tab portrait to landscape or in reverse the screen also changed.
But my requirement is irrespective of changing the mode either portrait to landscape to Landscape to portrait, my application should run in portrait mode only.
add android:screenOrientation="portrait" for each activity in your manifest.xml file.
You can do this programmatically too, for all your activities making an AbstractActivity that all your activities extends.:-
public abstract class AbstractActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
You can define mode in AndroidManifest.xml as follows,
android:screenOrientation="portrait" after the android:name=".activityName"
for example like this,
<activity android:name=".MainActivity" android:label="#string/app_name" android:screenOrientation="portrait">
and from activity class you can use following code,
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
You can use this for landscape
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
To change to portrait mode, use the
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
In your manifest file,
<activity android:name=".MainActivity" android:label="#string/app_name" android:screenOrientation="portrait">
Then add this to your java class:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
If you search first, you'll find this question answered already. I'll post the answer again here though.
Go to your manifest file, and under the activity that you want to keep portrait only, insert the following line
android:screenOrientation="portrait"
An example is given below.
<activity android:name=".YourActivity"
android:label="Your Activoity"
android:screenOrientation="portrait">
this will make your activity forced to be in portrait mode only, even if you hold the device in landscape mode.
i have one activity in my manifest.xml my activity is CPohonApp but in this activity i have 2 config
this is my manifest
<activity android:name=".pupuk.CPupukApp" android:configChanges="keyboardHidden|orientation"></activity>
and
<activity android:name=".pupuk.CPupukApp" android:screenOrientation="portrait"></activity>
when run this code is not work.
how to run two config in one name activity in manifest.xml?
thank you.
sorry for my bad english
Alex
Alex when you set android:screenOrientation="portrait" then your activity remains in portrait mode so no need to declare orientation configChanges. Try this.
If you have more than one configuration for particular activity then no need to declare it more than one time . Declare all those in that single activity.
<activity android:name=".pupuk.CPupukApp" android:configChanges="keyboardHidden" android:screenOrientation="portrait"></activity>
add the two configurations to single activity initialization
<activity android:name=".pupuk.CPupukApp" android:configChanges="keyboardHidden" android:screenOrientation="portrait"></activity>
I want to prevent my application from restarting when orientation of device changes. I have lock the app orientation as per below code but it doesn't help for the same.
<activity android:name=".CheckMemory"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:theme="#style/customTheme"
android:label="#string/app_name">
</activity>
and
#Override
public void onConfigurationChanged(final Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//Do nothing here
}
I did below and works well.
<activity android:name=".CheckMemory"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:theme="#style/customTheme"
android:label="#string/app_name">
</activity>
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
First of all, remember that screen orientation is defined per activity. So if you have other Activities than CheckMemory, they will still respond to a change in orientation. You need to define android:screenOrientation for all your Activities.
Secondly, you seem to be calling super.onConfigurationChanged(newConfig), will this not just do what the system will normally do?
Try to remove that line and really leave the method empty (really do nothing).