Android Application Resources

Source: Internet
Author: User

Reference style attributes

? [<Package_name >:] [<resource_type>/] <resource_name>

Apply the specified property value under the current topic
<Edittext id = "text"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textcolor = "? Android: textcolorsecondary"
Android: text = "@ string/hello_world"/>

Capture runtime configuration changes

Configuration changes such as screen orientation, keyboard validity, and language usually cause android to restart the running activity (ondestroy is called, followed by oncreate ).
1. Use the following methods to save data:

// Onretainnonconfigurationinstance is called between onstop and ondestory.

// The mydataobject object cannot contain any data (drawable, adapter, and view) associated with the context to prevent memory leakage.

// Unavailable bundle. Bundle needs to be serialized and deserialized when saving the status of large data volumes, which consumes a lot of memory and may slow down the configuration change process.
@ Override
Public object onretainnonconfigurationinstance (){
Final mydataobject DATA = collectmyloadeddata ();
Return data;
}

 

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Final mydataobject DATA = (mydataobject) getlastnonconfigurationinstance ();
If (Data = NULL ){
Data = loadmydata ();
}
...
}

2. You can also declare that you can capture the configuration changes during the runtime to prevent the Android system from restarting the activity.
<Activity Android: Name = ". myactivity"
Android: configchanges = "orientation | keyboardhidden"
Android: Label = "@ string/app_name">
When the declared runtime configuration changes, the onconfigurationchanged event will be received.

// When the configuration of an unspecified runtime changes, Android will restart the running activity
@ Override
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 ();
}
// Checks whether a hardware keyboard is available
If (newconfig. hardkeyboardhidden = configuration. hardkeyboardhidden_no ){
Toast. maketext (this, "keyboard visible", Toast. length_short). Show ();
} Else if (newconfig. hardkeyboardhidden = configuration. hardkeyboardhidden_yes ){
Toast. maketext (this, "keyboard hidden", Toast. length_short). Show ();
}
}

Related Article

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.