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