This article extracts from the Tekkaman blog.
" Activity Introduction "
An activity is an application component that provides a screen that facilitates interaction with the user. Each activity is given a window on which the user interface can be drawn. When an activity is created, its implementation of the OnCreate (Bundle savedinstancestate) method will be recalled by the system, usually in this method calls the Setcontentview (view view) method to load the display layout file UI, and use the Findviewbyid method to get the controls in the layout UI, modify the data, or populate the data.
"Activity life cycle"
An application is usually composed of multiple activity, and the activity in the system is managed by an activity stack. When a new activity is started, it is placed on top of the stack and becomes a running activity, and the previous activity remains in the stack and is no longer placed in the foreground until the new activity exits.
Activity has four essential differences in state:
1. At the front of the screen (activity stack top), called the active state or the running state
2. If an activity loses focus, it is still visible (a new non-full-screen activity or a transparent activity is placed on top of the stack), called a paused state (Paused). An activity in a paused state is killed when the system memory is extremely low.
3. If an activity is completely covered by another activity, it is called a Stop state (Stopped). It retains all state and member information, but it is no longer visible, so its window is hidden and stopped's activity is killed when the system memory needs to be used elsewhere.
4. If an activity is a paused or stopped state, the system can remove the activity from memory, and the Android system is removed in two ways, either requiring the activity to end, or directly killing its process. When the activity is displayed again to the user, it must restart and reset the previous state.
Several processes in the activity life cycle:
1. Start activity: The system calls the OnCreate method first, then calls the OnStart method, and finally calls Onresume,activity into the running state.
2. Current activity is overwritten or locked by other activity: the system invokes the OnPause method, pausing the execution of the current activity.
3. The current activity is returned to the foreground or unlock screen by the overwritten state: The system calls the Onresume method and enters the running state again.
4. The current activity goes to the new activity interface or press the home key to return to the main screen, itself back to the background: the system will first call the OnPause method, and then call the OnStop method, into a stagnant state.
5. The user backs back to this activity: the system calls the Onrestart method first, then calls the OnStart method, and finally calls the Onresume method, again into the running state.
6. Current activity is in a covered state or the background is not visible, that is, the 2nd and 4th steps, the system is out of memory, kill the current activity, and then the user returned to the current activity: Call OnCreate method Again, OnStart method, Onresume method To enter a running state.
7. The user exits the current activity: The system calls the OnPause method first, then calls the OnStop method, and finally calls the Ondestory method to end the current activity.
"Create Activity"
Inherits the activity class and its subclasses, implements the OnCreate method (and OnPause, etc.)
Public class extends Activity { @Override protectedvoid onCreate (Bundle savedinstancestate) { Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); = (Button) Findviewbyid (R.ID.MYBTN); }}
"Declare your activity in the manifest file."
Activity that the application first launches
<activity android:name= "com.***.activity. Mainactivity ">
<intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android: Name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity>
Other activity <activity android:name= "com.***.activity. Otheractivity "></activity>
"Start an activity"
The simplest method:
New Intent (mainactivity. this, otheractivity. class ); startactivity (intent);
Android Learning Trivia-activity Basics