Fourth three cornerstones of Android development-activity, service and handler (2)

Source: Internet
Author: User

Creation of 4.1.3Activity

Creating an Activity in Android is a simple matter, write a class that inherits from Android.app.Activity , and declare it in the androidmanifest.xml file . Below, let's take a look at an example where we create a myactivity class that inherits from activity, with the following code:


     

     

     

     

     

     

Here we rewrite the OnCreate method, in which we can do some initialization work, where main is the Main.xml layout file in the layout directory, where we define a TextView in this configuration file, the code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?>

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"

android:orientation= "Vertical"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent" >

<textview

Android:layout_width= "Fill_parent"

android:layout_height= "Wrap_content"

Android:text= "This is Activity 1"/>

</LinearLayout>

It is important to note that every activity we have to declare in Androidmanifest.xml before it can be used. Androidmanifest.xml in the <activity> node to explain activity, the APK file after installation, the system according to the instructions here to find read activity, the code is as follows:


     

     

     

     

     

It is necessary to note that The role of Android.intent.action.MAIN is to set myactivity as the primary activity, that is, the first activity;android.intent.category.launcher to start is to determine whether the application appears in the program list 。 Run effect 4-3 as shown:

Figure 4-3 Activity Creation

Four load modes for 4.1.4Activity

In multiple activity development of Android, the jump between activity may need to be in several ways: sometimes it is common to generate a new instance, sometimes you want to jump to the original activity instance, rather than generate a large number of repetitive activity. The load mode is the decision to start an activity in a different way.

In Android, there are 4 activation modes for the activity, namely:

1) Standard: Normal mode, call the StartActivity () method to produce a new instance.

2) Singletop: If an instance is already at the top of the activity stack, it does not produce a new instance, but simply invokes the newinstance () method in activity. If it is not at the top of the stack, a new instance is generated.

3) Singletask: this instance will be generated in a new task, which will be used for each invocation, and will not produce a new instance.

4) SingleInstance: This is basically the same as Singletask, there is only one difference, in this mode the activity instance in the task, can only have this activity instance, cannot have other instances.

These startup modes can be set in the Launchmode property of the Androidmanifest.xml file <activity>.

There are also flags in the relevant code that can be used, for example, if we want to enable only one instance, you can use the Intent.flag_activity_reorder_to_front flag. This flag indicates that if the activity has been started, it will not generate new activity, but just add the activity instance to the top of the stack, the code is as follows:

Intent Intent = new Intent (reorderfour.this, Reordertwo.class);

Intent.addflags (Intent.flag_activity_reorder_to_front);

StartActivity (Intent);

You can see that the load mode of the activity is controlled interactively by the property values of the activity's <activity> elements in the flag and Androidmanifest.xml files set in the intent object that initiates the activity, Flag has a higher priority when there is a conflict between them.

Fourth three cornerstones of Android development-activity, service and handler (2)

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.