Android onConfigurationChanged () not called in action

I understand that there are a couple of other posts on this topic, however the solutions for these posts do not work for me.

Basically, I want to stop my activity after restarting when the device orientation changes. To do this, I changed the action in the manifest file:

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

and I redefined onConfigurationChanged() in my work:

 @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); System.out.println("IN onConfigurationChanged()"); } 

However, activity still restarts when the orientation changes, and the onConfigurationChanged() method is not called.

Does anyone know why this might happen?

+8
java android android-activity orientation
source share
8 answers

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

+1
source share

You must use 13 APIs and set this configuration in your part of the manifest action: android: configChanges = "orientation | keyboardHidden | Screen size"

It works great. On all versions of Android.

+8
source share

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

+1
source share

Change your manifest to the next

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

and refer to this link for a detailed explanation of the orientation problem

+1
source share

if you define configchanges = orientation in your manifest, then the action will not be restarted, but instead, onConfigurationChanged will be called as you currently implemented it. First, try to record this using the Log Log (this is the correct way to register things in android, do not use System out, it is considered bad practice for this) and it’s super, but this is only a 1% chance that it will fix what is happening to you.

In the second case, you have the current activity nested in tabHost, for example, or in the Activity group. if there is parent activity in your activity, then you need to add a configuration change in this case, and a callback will occur.

If so, and you want to send the result or do something in this case, then you need to get a link to the parent of the parent and call the method for it to change.

0
source share

if you have a snippet you also need to:

 void setRetainInstance(boolean retain) 

Whether the fragment controls the instance is saved during the recreation of the activity (for example, from a configuration change).

I ran into this and set it to true, fixing it.

0
source share

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" /> 
0
source share

Change the onConfigurationChanged method to

 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(); } } 
-2
source share

All Articles