-2

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

Chirag Patel
  • 11,416
  • 3
  • 26
  • 38

2 Answers2

2

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);
}
Umer Farooq
  • 7,356
  • 7
  • 42
  • 67
0

try this one from 2.3.3+ :

android:configChanges="orientation|keyboardHidden|screenSize"
Udi Oshi
  • 6,787
  • 7
  • 47
  • 65
  • Try this android:configChanges="orientation|keyboardHidden|layoutDirection – Rupali Jan 27 '14 at 10:45
  • 1
    Can you add screenorientation to specific activity and check once as , – Rupali Jan 27 '14 at 11:05
  • Hey android:screenOrientation="portrait" is working for particular Activity. and it fixes my activity's orientation. But is there any solution without using this that orientation change will work and OnCreate() is also not calls. @RupaliG – Chirag Patel Jan 27 '14 at 11:20