Duplicate oncreate execution caused by activity switching in Android Development

Source: Internet
Author: User

When we use Android for development,The oncreate method is triggered when the activity is switched to the background or the layout is switched from landscape to portrait.

This can be avoided in Android development. We can use androidmanifest. adding the activit element in XML to this attribute Android: configchanges = "orientation | keyboardhidden" can effectively avoid repeated loading of the oncreat method,

 

The content of androidmanifest. XML is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. Demo"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Activity Android: Name = ". demogps"
Android: configchanges = "orientation | keyboardhidden"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
<Uses-library Android: Name = "com. Google. Android. Maps"/>

</Application>
<Uses-SDK Android: minsdkversion = "7"/>

<Uses-Permission Android: Name = "android. Permission. Internet"> </uses-Permission>
<Uses-Permission Android: Name = "android. Permission. access_coarse_location"> </uses-Permission>
<Uses-Permission Android: Name = "android. Permission. access_fine_location"> </uses-Permission>
 
</Manifest>

 

At the same time, the onconfigurationchanged (configuration newconfig) method is reloaded in the Java file of the activity, so that methods such as layout switching or window switching are not reloaded.CodeAs follows:

@ Override
Public void onconfigurationchanged (configuration newconfig)
{
Super. onconfigurationchanged (newconfig );
If (this. getresources (). getconfiguration (). Orientation = configuration. orientation_landscape)
{
// Land
}
Else if (this. getresources (). getconfiguration (). Orientation = configuration. orientation_portrait)
{
// Port
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.