Include at least one main interface screen in your application that handles the main UI functionality of your application. This main interface is typically composed of multiple fragment and is supported by a group of secondary activity. To switch between screens, you have to start a new activity. The general activity takes up the entire display, but can create a translucent or floating activity.
First, create activity
By inheriting the activity class, you can create an activity window with the following basic framework:
1 public class MyActivity extends Activity {2 @Override3 protected void onCreate (Bundle savedinstancestate) {4< C4/>super.oncreate (savedinstancestate); 5 }6}
The above code is an empty activity that can be created by using fragment, layout, and view. A view is a UI control that is used to display data and provide interactive interaction. Android provides multiple layout classes as ViewGroup, which can contain multiple views to help with the UI layout. Fragment is used to encapsulate parts of the UI, making it easy to create dynamic interfaces that can be rearranged in the direction of different screen sizes to optimize UI performance.
To assign a UI to an activity, you need to call the Setcontentview () method in the OnCreate () method. You can create a layout by creating it in Java code, or by calling an XML layout resource file. There are two different ways:
1 protected void OnCreate (Bundle savedinstancestate) {2 super.oncreate (savedinstancestate); 3 TextView TvShow = new TextView (this); 4 Setcontentview (tvShow); 5 tvshow.settext ("Hello"); 6 }
Of course, the way to create a UI by calling an XML layout file is more common, as follows:
1 protected void OnCreate (Bundle savedinstancestate) {2 super.oncreate (savedinstancestate); 3 Setcontentview (R.layout.activity_main); 4 }
Finally, the activity class is created so don't forget to register it in manifest. (Registration method and reason please refer to: Android Advanced programming note (ii) Manifest file node details)
Note that for an activity to be used by the Application launcher, it must contain a intent-filter that listens to the main action and the launcher classification, as follows:
1 <activity2 android:name= "com.codingblock.myactivity.MyActivity" 3 android:label= "@string/app_name" >4 <i Ntent-filter>5 <action android:name= "Android.intent.action.MAIN"/>6 <category android:name= "a Ndroid.intent.category.LAUNCHER "/>7 </intent-filter>8 </activity>
Ii. Lifetime of activity
A proper understanding of the lifetime of an activity allows for better management of resources for applications, thus making applications more coherent and fluid.
1. Activity stack
The state of each activity is determined by the position in which he is located in the activity stack, which is the last-in-first-out collection of all currently running activity. When a new activity starts, it becomes active and moves to the top of the stack, and when it returns to the previous activity, the foreground activity is closed, then the total next activity of the station moves to the top of the stack and becomes the activity state.
2. Activity status
As activity is created and destroyed, they undergo the following 4 possible states in the process of moving out of the stack:
• Active Status: When an activity is at the top of the stack, it is visible, with the focus of the foreground activity and can accept user input.
• Paused Status: Activity is visible but has no focus and cannot accept user input events. (for example, when a transparent or non-full-screen activity is in front of the activity)
• Stop Status: Activity is not visible. At this point, the activity remains in memory and all state information is kept, but the activity of such a state is prioritized when other parts of the system require the use of memory.
• Inactive Status: Activity is terminated. The activity has been removed from the stack at this time.
3. Monitor status change
To ensure that activity can react to state changes, Android provides a series of callback methods that are triggered when the state of the activity changes. The following code is the framework for the entire activity lifetime, and the description of each method is given in detail in the code comments:
1 public class MyActivity extends Activity {2 3//Start calling 4 @Override 5 protected void onCreate in full lifetime Bund Le Savedinstancestate) {6 super.oncreate (savedinstancestate); 7//Initialize activity and populate UI 8} 9 10/ /called after the OnCreate method is complete, to restore the UI state one by one @Override12 protected void onrestoreinstancestate (Bundle savedinstancestate) {13 Super.onrestoreinstancestate (savedinstancestate); 14/* 15 * Restore UI state from Savedinstancestate 16 * This The bundle is also passed to ONCREATE17 * Since the activity was last visible, only the system terminates the activity when it is called 18 */19}20 21//In subsequent Activit The y process is visible before the lifetime call to @Override23 protected void Onrestart () {Super.onrestart (); 25//load change, know activity in this Process is already visible 26}27 28//called at the beginning of the visible lifetime @Override30 protected void OnStart () {Super.onstart (); 32 Since the activity is visible, apply any required UI Change33}34 35//Call the @Override37 protected void at the beginning of the activity status lifetime onres Ume () {.Onresume (); 39/*40 * Resume activity is required, but suspended UI updates, threads, or processes that are suspended when it is inactive 41 * At the end of the activity state life cycle to save UI state Changes 42 */43}44 45//Changes in UI state saved to saveInstanceState46 @Override47 protected void onsaveinstancest Ate (Bundle outstate) {super.onsaveinstancestate (outstate); 49/*50 * If the process is terminated and restarted when it is run, 51 * then this bundle will be passed to OnCreate and onRestoreInstanceState52 */53}54 55//At the end of the activity status lifetime call the @Override57 protected void OnPause () {super.onpause (); 59/*60 * Suspend UI updates, threads, or CPU-intensive processes that do not require updates 61 * When activity is not active in the foreground 62 */63}64 65//At the end of the visible lifetime call @Override67 protected void OnStop () {6 8 Super.onstop (); 69/*70 * Suspend unwanted UI updates, threads, or processing 71 * When activity is not visible, save all edits or state changes, because after calling this method, Beijing The city may be terminated. 72 */73}74 75//At the end of the full lifetime call @Override77 protected void OnDestroy () {Super.on Destroy (); 79/*80 * Clean up all resources, including end thread, 81 * Close database connection, etc. 82 */83}84}
4. Understanding the lifetime of activity
Within the full lifetime of an activity from creation to destruction, it undergoes one or more repetitions of the active lifetime and the visible lifetime. Each conversion will trigger the above method handler.
• Full lifetime: the time range between the first call to OnCreate () and the Last Call to OnDestroy (). Sometimes an activity process terminates without invoking the OnDestroy method.
Using the OnCreate method to initialize the activity, if the activity terminates unexpectedly, the OnCreate method accepts a bundle object that contains the UI state that was saved when the onsaveinstancestate was last called. You should use this bundle to restore the UI to the last state, either through the OnCreate method or by overriding the onrestoreinstancestate.
Fast creation and destruction of objects can lead to additional garbage collection processes, and in order to ensure code efficiency, try not to create short-term objects. If the activity is creating the same set of objects in a regular way, consider creating it in OnCreate because OnCreate is called only once in the activity lifetime.
• Visible lifetime:onstart and OnStop time. At this point, the activity is visible, but may not have the focus, or it may be partially obscured. Activity may contain multiple visible lifetimes during the full lifetime. In extreme cases, the Android runtime may terminate an activity in its visible lifetime without invoking the OnStop method.
The OnStop method should be used to pause or stop animations, threads, sensor listeners, GPS lookups, timers, service, or other processes dedicated to updating the user interface. When the UI starts again, you can use the OnStart or Onrestart method to recover or restart these processes.
Onrestart is called immediately before all methods except the first call to the OnStart method. It can be used to complete special processing only when the activity is restarted within its full lifetime.
The Onstart/onstop method can also be used to register or unregister broadcast reciver that are specifically designed to update the user interface.
• Active lifetime:the time between Onresume and its corresponding onpause.
When activity is active, it is in the foreground and can receive user input events. Activities may experience multiple active lifetimes before they are destroyed, and in the event of loss of focus, the active lifetime is over. Try to get the code in the OnPause and Onresume methods to execute quickly and as little as possible. To ensure that the response is maintained when the front and rear stations are switched.
The Onresume method can be lightweight. Use it to re-register broadcast receiver or other processes that have been stopped using OnPause.
Http://www.cnblogs.com/codingblock/p/4757913.html
Android Advanced Programming Notes (iv) in-depth discussion of activity (RPM)