Continue with an article on the Android system. On an article: "Translation" Android system introduction
This article focuses on some of the key concepts of building Android apps:
Activity
Activity is a separate UI-based page (screen) in the application, for example: in an email application, there is an activity to display a list of email, and activity to display a specific email. In general, as long as there is a user interaction application, it will contain at least one activity.
An app can contain multiple activity, and each activity can interact with intent, and about intent is described later in this article.
An activity is generally inherited from android.app.Activity, and all activity is managed by the Onxx () method:
- onCreate ()-Set initialization method for activity
- OnStart ()-This method is called when activity becomes visible, and is called again when the activity stops and restarts;
- Onresume ()-Call this method when activity is visible and ready to receive and process user input;
- OnPause ()-This method is called when the activity is about to lose focus, which is quickly saved as committed data, stops CPU-intensive computing, and prepares for activity to run into the background;
- OnStop ()-The activity loses focus and this method is called. Used to release resources used by the application;
- Onrestart ()-This method is called when the activity is stopped and is needed again, and this method restores the status of the previous activity;
- OnDestroy ()-This method is called when activity is logged off, representing the end of the activity life cycle;
Activity life cycle:
Activity life Cycle Examples
Add in Strings.xml
<name= "Codeprojectweburl">http://www.codeproject.com</ string>
Override these methods of activity:
Public classFirstactivityextendsActivity {PrivateWebView Owebview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_first); Owebview=(WebView) Findviewbyid (R.id.webview); Owebview.loadurl (getString (R.string.codeprojectweburl)); } /**Called when the activity was about to become visible.*/@Overrideprotected voidOnStart () {Super. OnStart (); Toast.maketext (Getapplicationcontext (),"Inside OnStart", Toast.length_long). Show (); } /**called when the activity has become visible.*/@Overrideprotected voidOnresume () {Super. Onresume (); Toast.maketext (Getapplicationcontext (),"Inside Onresume", Toast.length_long). Show (); } /**called when another activity is taking focus.*/@Overrideprotected voidOnPause () {Super. OnPause (); Toast.maketext (Getapplicationcontext (),"Inside OnPause", Toast.length_long). Show (); } /**called when the activity is no longer visible.*/@Overrideprotected voidOnStop () {Super. OnStop (); Toast.maketext (Getapplicationcontext (),"Inside OnStop", Toast.length_long). Show (); } /**called just before the activity is destroyed.*/@Override Public voidOnDestroy () {Super. OnDestroy (); Toast.maketext (Getapplicationcontext (),"Inside OnDestroy", Toast.length_long). Show (); }}
Layout Add WebView Controls:
< WebView Android:layout_width = "Wrap_content" android:layout_height= "Wrap_content" android:id= "@+id/webview" android:layout_alignparenttop= "true" Android:layout_alignparentstart = "true" android:layout_alignparentbottom= "true" android: Layout_alignparentend= "true"/>
Operating effect:
After the OnCreate () method call, the OnStart () method is called, which we can see through the toast message;
When other activity is activated (such as by pressing the home key), the current activity invokes the OnPause () method, and the OnStop () method is immediately called;
Onresume () Called Scene: When the application is running, a phone call, the current application will lose focus (OnPause, onStop), the phone application will get the focus, when the call, the application to regain focus, this time the Onresume () method is called;
The OnDestroy () method is called when activity ends (calls to the OnFinish method) or the system temporarily destroys the activity instance for memory space.
PS: Still need to put activity, service, content provider, intent, fragement split into different articles, it is a bit long
Note:
1. This article in the translation process has been omitted;
2. Key words are not translated, so as not to confuse understanding;
3. Original: Http://www.codeproject.com/Articles/802449/Article
This article version history:
Initial version 2014.08.31
Introduction to "--activity" Android system