Android learning Activity preliminary, androidactivity

Source: Internet
Author: User

Android learning Activity preliminary, androidactivity

Activity, as the first step of Android development, summarizes the preliminary understanding in the study to facilitate future review and reflection.

1. When Android Studio is used to generate the first helloworld application to run on the mobile phone, it is found that the interface generated by MainActivity inherited from the Activity is invisible by default, even if android: Label = "This is FirstActivity" is defined in the AndroidManifest file. Later, I found out that it was the reason for inheriting the Activity, and changed it to extends AppCompatActivity.

2. Pay attention to the difference between @ + id/and @ String in the XML file. The "+" sign indicates that a unique identifier is to be built, and the "+" sign does not mean a reference.

3. Intent: acts as the transmitter of Activity and information. It has multiple constructor and has two methods: explicit Intent and implicit Intent:

Explicit Intent:

1 public void onClick (View view) {// click the button to execute the onClick method 2 Intent intent = new Intent (FirstActivity. this, SecondActivity. class); 3 startActivity (intent );

The Intent parameters are used to indicate the departure Activity and the Activity to be started. You can use the startActivity method to pass intent into this method to switch between the activities.

Implicit Intent:

The main part of the implicit Intent is not in the class file, but in the Activity registration of Androidmanifest:

<activity android:name=".ThirdActivity">            <intent-filter>                <action android:name="android.intent.action.VIEW"/>                <category android:name="android.intent.category.DEFAULT"/>                <data android:scheme="http"/>            </intent-filter>        </activity>

Note that there are two parameters in intent-filter: action and category (each action can have only one parameter, while category can have multiple parameters). These are equivalent to one internal feature of ThirdActivity, when using the implicit intent in the code, you need to "match" to get the correct running method:

public void onClick(View view) {                Intent intent = new Intent("android.intent.action.VIEW");                intent.addCategory("android.intent.category.DEFAULT");                startActivity(intent);

The Intent constructor uses action as the parameter. If category is set to default, you do not need to write it out. In this way, the features of the two are "matched" and the Activity switching effect is the same as that of the explicit intent. NOTE: If category is registered in the manifest file in a custom way, the following addCategory method must be used for description in the Intent method:

intent.addCategory("android.intent.category.MY_DEFAULT");

In this way, the manifest file and the intent transfer information can be "matched" to start the target Activity correctly.

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.