Chapter 4 three cornerstones of Android development: Activity, Service, Handler (2), and androidhandler

Source: Internet
Author: User

Chapter 4 three cornerstones of Android development: Activity, Service, Handler (2), and androidhandler
4.1.3Activity Creation

It is easy to create an Activity in Android.Android. app. ActivityAndAndroidManifest. xml fileDeclare it. Next, let's take an example. We create a MyActivity class that inherits from the Activity. The Code is as follows:

public class MyActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);      
        setContentView(R.layout.main); 
    } 
} 

 

Here, we overwrite the onCreate method. In this method, we can perform some initialization work, where main is the main under the layout directory. xml layout file. Here, we define a TextView in this layout 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>

 

Note that every Activity must be declared in AndroidManifest. xml before it can be used. In AndroidManifest. xml, the <activity> node is used to describe the Activity. After the apk file is installed, the system searches for and reads the Activity based on the instructions here. The Code is as follows:

<activity android:name=". MyActivity " android:label="@string/app_name"> 
          <intent-filter> 
                   <action android:name="android.intent.action.MAIN" /> 
                   <category android:name="android.intent.category.LAUNCHER" /> 
          </intent-filter> 
 </activity> 

 

Android. intent. action. MAIN is used to set MyActivity as the MAIN Activity, that is, the first Activity to be started; android. intent. category. LAUNCHER determines whether an application is displayed in the program list. The running effect is as follows:

Figure 4-3 Activity Creation

 

4.1.4Activity four loading modes

In the development of multiple Android activities, there may be many ways to jump between activities: sometimes it is common to generate a new instance, sometimes you want to jump to an original Activity instance, instead of generating a large number of repeated activities. The loading mode determines the method to start an Activity.

In Android, there are four Activity startup modes:

1) standard: standard mode. A new instance is generated after the startActivity () method is called.

2) singleTop: if an instance already exists at the top of the Activity stack, a new instance is not generated, but the newInstance () method in the Activity is called. If it is not at the top of the stack, a new instance is generated.

3) singleTask: this instance is generated in a new task. This instance will be used for each call in the future and no new instances will be generated.

4) singleInstance: this is basically the same as singleTask. There is only one difference. In this mode, the task of the Activity instance can only have this Activity instance, but not other instances.

You can set these startup modes in the launchMode attribute of <activity> In the AndroidManifest. xml file.

Some logos can also be used in related Code. For example, if you 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, a new Activity will not be generated, but the Activity instance will be added 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 );

 

The loading mode of the Activity is determined by the Flag and AndroidManifest set in the Intent object of the started Activity. interaction control of the attribute values of the <Activity> elements of an activity in an xml file. When there is a conflict between the elements, the Flag has a higher priority.

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.