Why is onCreate () called after onConfigurationChanged?

I created an activity and two layouts for activity, one for landscape and portrait mode. Both layouts have the same views and identifiers. In the manifest file, I added android:configChanges="orientation" in action.

Now, according to the documentation, the action does not restart if I mention configChanges and turn on the onConfigurationChanged method.

 onCreate(Bundle savedInstanceState) { Log.i("Message","inside oncreate()") ; } onConfigurationChanged(Configuration newConfig) { Log.i("Message","inside onconfigurationchanged()") ; } 

My log shows both messages when I change orientation. Is there a way to stop the onCreate () method when called when changing orientation?

+4
source share
2 answers

perhaps you could add more configuration changes that you want to ignore, starting from running a specific API on android, the orientation consists of other flags, and the documentation says "orientation":

Changed the screen orientation - the user turned the device. Note. If your application targets API level 13 or higher (as indicated by the minSdkVersion and targetSdkVersion attributes), then you should also declare the “screenSize” configuration as it also changes when the device switches between portrait and landscape orientation.

so please try using and tell us if this helped:

 android:configChanges=orientation|screenSize" 

here's the documentation for "screenSize":

The current available screen size has changed. This represents a change in the current size compared to the current aspect of the relationship, so it will change when the user switches between the portrait. However, if your application targets API level 12 or lower, then your activity always processes this configuration change (this configuration change does not restart your activity, even if it runs on an Android 3.2 or higher device). Added to API Level 13.


EDIT: here is 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> 
+7
source

The official documentation says that the “orientation” in the manifest is able to prevent a reboot when the screen orientation and “keyboardHidden” change, to prevent a reboot when the keyboard availability changes. All you have 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 this does not work, try changing the " keyboardHidden " to " screenSize ".

+1
source

Source: https://habr.com/ru/post/1488675/


All Articles