Orientation changes calls onCreate() method every time in android Kitkat - android

I already know that to stop being called OnCreate method on change of orientation in android.
and below is the code for that too.
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden" />
But Its not working in my Google Nexus 5. Because it has the android kitkat os. Where as in Samsung Mega android 4.2 its working.
So what is the code for android kitkat to stop being call OnCreate() on orientation change.
I also fixed orientation using below code
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/AppTheme" >
But that code is also not working.
(Edited) To fix orientation I need to add android:screenOrientation="portrait"code in Activity tag. Thanks #RupaliG
Thanks

Use a flag check in onCreate. Make flag=true at the time of initialization/declaration
add android:configChanges="orientation" in ur manifest file
In your Java file override the onConfigurationChanged method and make the flag as false.
After doing so ur onCreate will be called but the code mention in if won't be called.
Move ur code inside the if condition.
try using this.
static boolean flag = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(!flag)
return;
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
Log.d("ONCONFIGCHANGE", "CALLED" );
flag = false;
super.onConfigurationChanged(newConfig);
}

try this one from 2.3.3+ :
android:configChanges="orientation|keyboardHidden|screenSize"

Related

Orientation change notification in Android

Trying to follow something. Boiling down to simplest: In my AndroidManifest.xml I have the following:
<application
android:allowBackup="true"
android:configChanges="orientation"
...
</application>
In my activity that runs I have this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
Log.d("PrimesAreInP", "onConfigurationChanged called");
}
However when I change the orientation of the phone from portrait to landscape I never see the onConfigurationChanged() print. What else do I need to do to see this call?
Try adding this for activity tag:
<activity android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboa‌​rdHidden"
I think you are missing this dude :
super.onConfigurationChanged(newConfig);
and not forget to write this line in activity tag of your menifest :
android:configChanges="orientation|screenSize"
good luck !!!

Android orientation change even after locking it in system settings

as in the topic, ive got a trouble with locking screen orientation after its been locked in device's settings. Its a landscape app. Ive read couple threads about similar problem but havent got any revelant answer.
Thanks in advance
Mac
You can add android:screenOrientation="landscape" in your activity tag to specify the mode of screen orientation in your manifest file
<activity
android:name="com.xyz.myapp.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="landscape" >
Hope it helps .
Add setRequestedOrientation in the onCreate method like below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.homepage);
This would set to landscape if auto_rotate is off, otherwise if would rotate normally as long as it wasn't set to land/port in the manifest.
if(android.provider.Settings.System.getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0) != 1){
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

Locking orientation to portrait force close in my phone

I want my app to lock into portrait mode. For this I used this code :
<activity android:name="MyActivity"
android:label="#string/app_name" android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
>
and in MyActivity class :
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
This code work correctly in emulator, but when I install and run in my phone, the app is force closed.
How can I solve this problem?
Yo already use in manifest then there is no need to use by pragmatically. So remove the onConfigurationChanged method from your code.
I am using
android:screenOrientation="portrait"
in the manifest file, no other code in Java side and is working.
I see two problems here:
Remove android:configChanges="orientation|keyboardHidden" from manifest
Remove onConfigurationChanged from java
and it should work just fine

Disable screen rotating on Android [duplicate]

This question already has answers here:
How do I disable orientation change on Android?
(13 answers)
Closed 10 years ago.
When I press a button, I would like to disable screen rotation on all my activities. How can I do that?
BTW, the phone can be located in landscape or portrait position when the user click the button.
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
You can change your AndroidManifest.xml to
<activity android:name="MainActivity" android:configChanges="orientation">
Which informs the OS that you will handle these config changes (by doing nothing.)
See How do I disable orientation change on Android?
I've found the solution:
in manifest.xml:
<application android:icon="#drawable/icon" android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar" android:name="MyApplication">
in MyApplication.java:
public class UPackingListApplication extends Application {
public void onConfigurationChanged(Configuration newConfig) {
newConfig.orientation = [orientation we wanna to use (according to ActivityInfo class)];
super.onConfigurationChanged(newConfig);
}
}

Android - Vertical layout only

How do I make sure my app is only for vertical layout?
I have tried android:screenOrientation="portrait" but that doesn't seem to do the trick.
You need to add to all your activity not for one only. I think you understood that setting is per application wide, but it isn't.
<activity android:name=".MyActivity"
android:label="My Activity"
android:screenOrientation="portrait">
Add the declaration to the activity tag in AndroidManifest for every Activity you want to be portrait-only.
If you want some group of your activities to be locked on PORTRAIT mode only, than you can choose the next way:
public abstract class BasePortraitActivity extends Activity {
#Override
protected final void onCreate(Bundle state) {
super.onCreate(state);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
performOnCreate(state);
}
protected abstract void performOnCreate(Bundle state);
}
And than just extend BasePortraitActivity where you need it. Or just add setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); to YourActivity.onCreate().
you have to change into AndroidManifest.xml.
for each activity you have to insert:
android:configChanges = "orientation"
android:screenOrientation = "portrait"
for e.g.:
<activity android:name=".YourActivityName"
android:label="#string/app_name"
android:configChanges = "orientation"
android:screenOrientation = "portrait">
This works for a single activity.. But there seems no application wide setting there.
In your manifest under activity add this :
<activity
android:name="com.zeus.MyProject"
android:screenOrientation="portrait"
>
Activity is to block just in case want to block all and only repeat this line of code in their Activitys.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Here, talk to the java does not want that the user can rotate their activity.
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
or open your "AndroidManisfest.xml" and add the rows for the portrait mode as shown below.
android:configChanges="orientation"
android:screenOrientation="portrait">
Add android:configChanges="keyboardHidden|orientation" to the activity.
Put this attribute in layout root:
android:orientation="vertical"
It may help.
Just to confirm Pentium10's answer.
I was having a similar issue and adding android:screenOrientation="portrait" to the activity tag.
Did the trick for me.

Categories

Resources