开发者

Android onConfigurationChanged() is not being called in Activity

开发者 https://www.devze.com 2023-03-13 00:03 出处:网络
I realize that there are a couple other posts on this topic, however the solutions for those posts are not working for me.

I realize that there are a couple other posts on this topic, however the solutions for those posts are not working for me.

Basically, I want to cease my Activity from restarting upon a device orientation change. To do this, I have modified the activity in the manifest file:

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

and I have overridden onConfigurationChanged() in my Activity:

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

    System.out.println("IN onConfigurationChanged()");
}

However, the activity is still res开发者_开发技巧tarting upon an orientation change, and the onConfigurationChanged() method is not being called.

Does anyone know why this may be happening?


You should use 13 API and set this configuration in your activity's part of the manifest: android:configChanges="orientation|keyboardHidden|screenSize"

It works fine. At all Android version.


Change your manifest to following

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

and refer this link for detailed explanation orientation issue


The only thing that worked was using getLastNonConfigurationInstance(). http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance()


You should not use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); method call any where in your application this will avoid calling onConfigChanged() method.


if you define configchanges=orientation in your manifest then the activity won't restart but instead onConfigurationChanged will be call as you have currently have it implemented. First try to log that with the log class Log (this is the proper way to log things in android don't use System out for this its considered a bad practice) and before super but that is just a 1% chance it will fix what is happening to you.

The second case is that you have the current activity nested in a tabHost for example or Activity Group. if your activity has a parent activity then configuration change needs to be added in that one and the callback will happen there.

If that is the case and you want to forward the result or also do something in the child then you need to get a reference to the child in the parent and call a method on it for the changes.


if you have a fragment then you need this also:

void setRetainInstance(boolean retain) 

Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change).

i ran into this and setting it to 'true' fixed it.


I used this, and it helped:

package="com.s2dio.evallet"
android:versionCode="1"
android:versionName="1.0" >

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


Modefiy your onConfigurationChanged method to the following

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();
    }
}
0

精彩评论

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

关注公众号