The oncreate method will be re-called when switching between landscape and landscape screens. You can use the following method to call the oncreate method during the switchover process. If you need to use different layout files for landscape and landscape screens, this method may not work. After I write code for testing, if the current screen is a portrait screen, it will not use the layout file of the landscape screen after switching, instead, I used the layout file of the portrait screen in the landscape. I don't know whether it was inappropriate or I did it. I hope to discuss this with you. View android
The API can tell that Android: onconfigurationchanged actually corresponds to the onconfigurationchanged () method in the activity. The appeal code added to androidmanifest. xml indicates that the oncreate () method is not executed when the screen direction is changed, the software disk is popped up, and the soft keyboard is hidden, but the onconfigurationchanged () method is executed directly (). If you do not declare this code segment, the oncreate () method will be executed according to the activity lifecycle, while the oncreate () method usually performs initialization before the display. Therefore, if the oncreate () method is executed for all operations such as changing the screen direction, it may cause repeated initialization and reduce program efficiency, in addition, data may be lost due to repeated initialization. This must be avoided by ten millions.
Do not call the oncreate method: add the Android: configchanges = "orientation | keyboardhidden" attribute or add this function to the activity element of mainifest. xml.
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Test program I wrote:
package lzu.LandAndPortraintTest;import android.app.Activity;import android.content.pm.ActivityInfo;import android.content.res.Configuration;import android.os.Bundle;public class LandAndPortraintTest extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); printSentence(); } public void printSentence(){ System.out.println("printSentence"); } public void printPortraintSentence(){ System.out.println("printSentence Portraint"); } public void printLandscapeSentence(){ System.out.println("printSentence Landscape"); } public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { printLandscapeSentence(); } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { printPortraintSentence(); } }}
Main. xml file in Layout-land:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /></LinearLayout>
Main. xml file in Layout-Port:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/app_name" /></LinearLayout>
Strings. xml file under values:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, LandAndPortraintTest!</string> <string name="app_name">LandAndPortraintTest</string></resources>
Result: