R4chi7 R4chi7 - 1 month ago 6
Android Question

why is onCreate() being called after onConfigurationChanged?

I have created an activity and two layouts for the activity, one each for landscape and portrait mode. Both the layouts have same Views and IDs. In the manifest file, I have added

android:configChanges="orientation"
in the activity.

Now, according to the documentation, the activity is not restarted if I mention configChanges and include the
onConfigurationChanged
method.

onCreate(Bundle savedInstanceState) {
Log.i("Message","inside oncreate()") ;
}

onConfigurationChanged(Configuration newConfig) {
Log.i("Message","inside onconfigurationchanged()") ;
}


My log is showing both messages when i change the orientation. Is there any way to stop the onCreate() method from being called when the orientation changes?

Thanks in advance.

Answer

maybe you could add more configurations changes that you wish to ignore, since starting of a certain API on android, the orientation consist of other flags , as the documentation says about "orientation" :

The screen orientation has changed — the user has rotated the device. Note: 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.

so, please try to use , and tell us if it helped :

android:configChanges=orientation|screenSize"

here's the documentation about "screenSize":

The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. 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). Added in API level 13.


EDIT: here's my simple code to show that it works:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("Message","inside oncreate()");
    }

    @Override
    public void onConfigurationChanged(final Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.i("Message","inside onconfigurationchanged()");
    }
}

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.test" android:versionCode="1"
  android:versionName="1.0">

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

  <application android:allowBackup="true" android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" android:theme="@style/AppTheme">
    <activity android:name="com.example.test.MainActivity"
      android:label="@string/app_name" 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>