R4chi7
R4chi7

Reputation: 863

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?

Upvotes: 4

Views: 4584

Answers (2)

android developer
android developer

Reputation: 116352

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>

Upvotes: 8

ohyes
ohyes

Reputation: 3328

Official documentation said that "orientation" in manifest is able to prevent restarts when the screen orientation changes and "keyboardHidden" to prevent restarts when the keyboard availability changes. All you need to do is declare the following codes in your manifest:

<activity android:name=".MyActivity"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">

It works for my project. If it doesn't work, try to change "keyboardHidden" to "screenSize".

Upvotes: 1

Related Questions