开发者

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 savedInstanc开发者_运维技巧eState) {
    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.


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+


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.


Try use this one.....

android:configChanges="orientation|keyboardHidden|screenSize"


You should change the configChanges entry in AndroidManifest.xml to:

android:configChanges="keyboardHidden|orientation"

Otherwise, sliding the keyboard doesn't trigger onConfigurationChange() even though the orientation changes. I just tested this on my HTC Desire Z.


Add this to your manifest to each activity.

android:configChanges="keyboardHidden|orientation|screenSize"

Then, override onConfigurationChanged on your activity as such

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


Manifest:

<activity
      android:name=".MainActivity"
      android:configChanges="orientation|keyboardHidden|screenSize">
</activity>

Activity & Fragment:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Log.d(TAG, "onConfigurationChanged  " +
               (newConfig.orientation
                == Configuration.ORIENTATION_LANDSCAPE
                ? "landscape" : "portrait"));
}

Output:

10-29 21:53:26.951 D/FragmentOne: onConfigurationChanged landscape

10-29 21:53:26.951 D/MainActivity:onConfigurationChanged landscape


It wasn't triggering in my Fragment (Fragments aren't registered in the AndroidManifest.xml) so I had to move it to the Activity managing my Fragment, in my case the TabsPagerActivity.


Few things can be cross check:

In my case I was tracking language change of device using the onConfigurationChanged.

Few things need to know about onConfigurationChanged is there is some difference in behavious between onConfigurationChanged of Activity and Application.

When you change the configuration of device the Application level onConfigurationChanged will be call automatically and immediately but onConfigurationChanged of activity will call when you navigate to that activity.

and another thing is only locale in manifest will not work sometime So you have declare other config change event along with the locale(in my case) that should be like android:configChanges="layoutDirection|locale"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜