Android Study Notes 38: Activity of four Android Components

Source: Internet
Author: User

Android applicationsProgramThe four components are activity, service, broadcastreceiver, and contentprovider. This article will give a comprehensive summary of the activity. Service and broadcasereceiver will also be introduced in the subsequent blog. For details about how to use contentprovider, refer to the blog "android Study Notes 37: Using content providers to share data".

 

1. register an activity

Activity is the most common component in Android. Every activity is equivalent to a screen, providing users with a visual interface for interaction. Applications can contain one or more activities as needed. These activities inherit from the activity classes under the Android. app package, and the operations between these activities are independent of each other.

All activities used in the application must be registered in the androidmanifest. xml file. When registering an activity, you must use the <activity> </activity> label, which is located in <Application> </Application>. The specific location is as follows:

 1  < Manifest  Xmlns: Android  = "Http://schemas.android.com/apk/res/android"  2   Package  = "Com. example. android_datastorage_sqlite"  3   Android: versioncode  = "1"  4   Android: versionname  = "1.0"   >  5  6  <  Application  7  Android: icon  = "@ Drawable/ic_launcher"  8   Android: Label  = "@ String/app_name"  9   Android: Theme  = "@ Style/apptheme"  >  10  11  < Activity  12  Android: Name  = ". Mainactivity"  13   Android: Label  = "@ String/title_activity_main"   >  14  <  Intent-Filter  >  15  <  Action Android: Name  = "Android. Intent. Action. Main"   />  16  <  Category  Android: Name  = "Android. Intent. Category. launcher"   />  17  </  Intent-Filter  >  18  </ Activity  >  19  20  </  Application  >  21  22  </  Manifest  >   

1.1 <activity> tag Attribute Value

In the <activity> </activity> label, you can set the following common attribute values:

(1) Android: name, which indicates the name of the custom activity subclass. The value can be the full name of the subclass. example. android_datastorage_sqlite.mainactivity ", you can also use". ". mainactivity ").

(2) Android: process. The attribute value indicates the process in which the activity component should run. Generally, this attribute is not set and all components are running in the same process. If the value starts with ":", a private new process is created for the activity. If the value starts with a lowercase letter, a global new process is created.

(3) Android: permission, which specifies the permissions required to start the activity.

(4) Android: screenorientation. This attribute value is used to set the screen direction. You can select the following options: unspecified (default value: Direction selected by the system), landecape (horizontal display, width is greater than height), portrait (portrait display, height is greater than width), user (use the user's preferred direction currently), behind (using the same direction as the activity under the activity in the activity stack), sensor (the display direction is determined by the device's direction sensor) nosensor (the display direction of the screen does not refer to the physical direction sensor ).

(5) Android: launchmode. The attribute value is used to set the Startup Mode of the activity. You can select standard, singletop, singletask, and singleinstance.

(6) Android: theme, which is used to set the theme mode of the activity.

1.2 embedded tags of <activity> tags

In the <activity> </activity> label, you can embed the following two labels:

(1) <intent-filter> </intent-filter>, which specifies the filtering rules of the activity component.

(2) <MEDA-data>, which specifies the additional data value provided to the activity component.

1.3 How to register activity under different packages

When an application contains multiple activities and these activities are not in the same package, you need to make a small change when registering the activity.

For example, the project shown in Figure 1 contains three activities in different packages.

Figure 1 project directory structure

When registering adddialogactivity and deletedialogactivityCodeThe same method as registering mainactivity does not work. Because the above Code specifies package = "com. example. android_datastorage_sqlite", you can use Android: Name = ". mainactivity" to register mainactivity. But obviously, adddialogactivity and deletedialogactivity are not included in the package "com. example. android_datastorage_sqlite ", so we should use Android: Name = ". useroerate. adddialogactivity "and Android: Name = ". useroerate. to register the adddialogactivity and deletedialogactivity components.

 

2. Activity Lifecycle

The lifecycle of an activity contains three stages and seven methods. For more information, see the blog "android Study Notes 04: Activity and activity lifecycle".I will not go into details here.

 

3. Two declaration methods of activity content

There are two ways to declare the activity display content. One is to declare the content through an XML layout file, and the other is to set the screen to an object inherited from the View class.

3.1 XML layout file Declaration

The XML layout file of the activity is located in the layout directory under the res directory of the project. A Layout file is equivalent to a view container, you can add a built-in view (such as button and textview) in the Android system, or add a custom subclass object that inherits the View class, or even a view container.

In addition, you can specify the arrangement of view objects in the view container in the XML layout file (such as linear layout linearlayout ).

It is really convenient to integrate different view objects through the XML layout file, but the autonomy left to developers is not enough. Especially for game programming, the view objects in the Android system cannot fully meet the design requirements. In this case, you generally need to inherit and extend the View class to develop the user interface you want.

3.2 Declaration through the subclass object of the View class

You can use the subclass object of the View class as the content to be displayed in the activity by performing the following two steps:

(1) Compile a myview class that inherits the View class. The ondraw (canvas) method must be implemented in the myview class. In this method, you can operate the canvas object to draw the interface.

(2) After drawing the interface, you can load the myview object in the activity by using the setcontentview (view) method to display the drawn interface.

For the specific implementation methods of the above two steps, refer to the blog post "android learning notes 09: simple application of painting and canvas".

 

4. Communication between activities

Intent objects are the carriers for communication between components. Communication between components means that intent objects are continuously transmitted. Intent objects can not only run between the same components (such as communication between activities), but also between different components (such as communication between activities and services ).

For activity components, intent mainly calls context. startactivity (), context. startactivityforresult () method is used to implement the transfer. The result is to start a new activity or start a new task for the current activity.

For details about how to use the preceding two methods to implement communication between activities, refer to the blog "android study note 33: intent introduction and how to use intent in activity".

 

5. Save the temporary activity status data

The onsaveinstancestate () method is provided in the activity for the current activity without your permission (such as a paused or stopped activity, when the system resources are extremely scarce, may be killed.) Save the temporary status data in the current activity when it is destroyed.

By using the onsaveinstancestate (bundle outstate) method, you can store the temporary status data of the activity in the bundle object as a key-value pair before the activity is killed, so that you can call oncreate () again () method to restore the activity status.

The following code uses the onsaveinstancestate () method to save the input content of the edittext object, and restores the data in the edittext object in the oncreate () method.

 1 Edittext medittext; // Edittext object, used for input content  2       Private   Static   Final String key1 = "edittextvalues "; //  Key used to save the content of the edittext object  3       Private   Static   Final String tag = "mainactivity "; //  Log output filter  4       5      /*  6   * Function: oncreate method, called during activity Creation  7   * Author: blog Park-still indifferent  8        */  9       Public   Void  Oncreate (bundle savedinstancestate ){  10           Super  . Oncreate (savedinstancestate );  11  Setcontentview (R. layout. activity_main );  12 Medittext = (edittext) This  . Findviewbyid (R. Id. edittext );  13           14           //  If the bundle object contains the content of the required object, the content of the object will be restored.  15           If (Savedinstancestate! = Null )&& Savedinstancestate. containskey (key1 )){  16  Medittext. settext (savedinstancestate. getstring (key1 ));  17   }  18 Log. I (TAG, "--> oncreate ()" );  19   }  20   21       /*  22   * Function: onsaveinstancestate method, called when activity is destroyed  23   * Author: blog Park-still indifferent 24        */  25       Protected   Void  Onsaveinstancestate (bundle outstate ){  26           Super  . Onsaveinstancestate (outstate );  27 String edittextvalues = medittext. gettext (). tostring (); //  Get edittext object content  28 Outstate. putstring (key1, edittextvalues ); // Save edittext object content as a key-Value Pair  29 Log. I (TAG, "--> onsaveinstancestate ()" );  30 }

Note that the onsaveinstancestate () method is not always called. The onsaveinstancestate () method is called only when the activity is forcibly destroyed to save memory resources () to save some temporary data instead of persistent data. To save persistent data, use the onpause () method.

 

6. Activity topic Mode

As mentioned above, you can set the theme mode of an activity by setting the Android: Theme attribute value under the <activity> </activity> label.

The following theme modes are preset in the Android system:

(1) Android: theme = "@ Android: style/theme. Dialog", display an activity as a dialog box

(2) Android: theme = "@ Android: style/theme. notitlebar". The application title bar is not displayed.

(3) Android: theme = "@ Android: style/theme. notitlebar. fullscreen". The application title bar is not displayed, and the activity is displayed in full screen.

(4) Android: theme = "@ Android: style/theme. Light", set the background of the activity to white

(5) Android: theme = "@ Android: style/theme. Light. notitlebar": sets the background of the activity to white and does not display the application title bar.

(6) Android: theme = "@ Android: style/theme. light. notitlebar. fullscreen ", sets the activity background to white, does not display the application title bar, and displays the activity in full screen

(7) Android: theme = "@ Android: style/theme. Black", set the activity background to black

(8) Android: theme = "@ Android: style/theme. Black. notitlebar", sets the activity background to black and does not display the application title bar

(9) Android: theme = "@ Android: style/theme. black. notitlebar. fullscreen ", sets the activity background to black, does not display the application title bar, and displays the activity in full screen

(10) Android: theme = "@ Android: style/theme. Wallpaper", with the system desktop as the background of the activity

(11) Android: theme = "@ Android: style/theme. wallpaper. notitlebar", which uses the system desktop as the background of the activity and does not display the application title bar

(12) Android: theme = "@ Android: style/theme. wallpaper. notitlebar. fullscreen "uses the system desktop as the background of the activity. The application title bar is not displayed, and the activity is displayed in full screen.

For example, you can set Android: theme = "@ Android: style/theme. Dialog" to simulate an activity as the dialog box dialog, as shown in figure 2.

Figure 2 activity in dialog box mode

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.