0

I have created an activity which has two layout define one in layout-port and another in layout-land. They work fine so far.

but the problem is I dont want that onCreate gets called again when the orientation gets changed. so to prevent that I have specified android:configChanges="orientation" attribute in Manifest file and use the following code in activity

@Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);

    }

now when I run the application and change the orientation on a real device I can see android is only using portrait layout even I tilt the device and not changing the activity state.

so basically onCreate is not getting called which I want but the device is not using landscape mode when the device gets tilted.

AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
Hunt
  • 8,215
  • 28
  • 116
  • 256
  • 1
    http://stackoverflow.com/questions/8532614/onconfigurationchanged-is-not-being-called-on-orientation-change – Omarj Nov 19 '12 at 15:40

1 Answers1

0

add android:configChanges="orientation|screenSize" to your activity in manifest.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.orientationchange"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"            
            android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    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();
        }
    }
}
Talha
  • 12,673
  • 5
  • 49
  • 68