Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onConfigurationChanged not being called

I am having trouble with telling Android to not call onCreate() when the orientation changes. I have added android:configChanges="orientation" to my manifest but still when the orientation changes onCreate() is called. Here is my code.

AndroidManifest.xml

<activity android:name="SearchMenuActivity" android:theme="@android:style/Theme.NoTitleBar" android:configChanges="orientation"></activity>

SearchMenuActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set the current layout to the search_menu
    setContentView(R.layout.search_menu_activity);

    Log.d(TAG, "onCreate() Called");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    //don't reload the current page when the orientation is changed
    Log.d(TAG, "onConfigurationChanged() Called");
    super.onConfigurationChanged(newConfig);
}

And my LogCat Output

06-23 12:33:20.327: DEBUG/APP(2905): onCreate() Called
//Orientation Changes
06-23 12:33:23.842: DEBUG/APP(2905): onCreate() Called

Does anyone know what I am doing wrong? Thanks.

like image 237
Stefan Bossbaly Avatar asked Jun 23 '11 16:06

Stefan Bossbaly


3 Answers

This was my gremlin for the ~same problem:

Caution: Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". 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).

(From http://developer.android.com/guide/topics/resources/runtime-changes.html)

TL;DR: add "|screenSize" to android:configChanges="orientation" for API 14+

like image 91
nmr Avatar answered Nov 07 '22 00:11

nmr


A couple of things to try:

android:configChanges="orientation|keyboardHidden|screenSize" rather than android:configChanges="orientation"

Ensure that you are not calling setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); anywhere. This will cause onConfigurationChange() to not fire.

Check that you are not using android:screenOrientation in your manifest.

If none of that works, read through the Android doc on handling runtime changes and make sure you are doing everything correctly. There may be something somewhere else in your code that's causing the problem. http://developer.android.com/guide/topics/resources/runtime-changes.html

EDIT: As derrik pointed out, I assumed that you were changing the configuration with the accelerometer detecting what way the device was facing. If you want the configuration to change as the keyboard is shown/hidden the configChanges in the manifest must include keyboardHidden as well.

like image 64
shanet Avatar answered Nov 07 '22 02:11

shanet


Try use this one.....

android:configChanges="orientation|keyboardHidden|screenSize"
like image 10
sonida Avatar answered Nov 07 '22 00:11

sonida