Android Learning Path--android Four components activity (i)

Source: Internet
Author: User

First, what is activity?

Activity simply means an interface. Every interface we see on an Android phone is an activity.



Ii.creation of activity

1. Define a class to inherit activity, and then register the activity in the application node of the manifest file Manifest.xml file. This activity was created successfully.

public class MyActivity extends Activity {}


2. Inventory File Brochure Activity
<application        android:allowbackup= "true"        android:icon= "@drawable/ic_launcher"        android:label= "@ String/app_name "        android:theme=" @style/apptheme ">        <activity            android:name=" Com.example.study.MainActivity "            android:label=" @string/app_name ">            <intent-filter>                < Action android:name= "Android.intent.action.MAIN"/>                <category android:name= " Android.intent.category.LAUNCHER "/>            </intent-filter>        </activity>                <activity Android:name= "Com.yang.study.MyActivity" >        </activity>    </application>
The mainactivity is the program-initiated activity, which can be seen from the action of Intent-filter

Our custom activity is the following myactivity

<intent-filter>                <action android:name= "Android.intent.action.MAIN"/>                <category android: Name= "Android.intent.category.LAUNCHER"/>            </intent-filter>
In particular, each application can actually create multiple icons in the launcher of launcher, the Android phone, just to configure the above Intent-filter in the activity, and of course no one is doing it. Multiple app icons easy to cause users to trouble.


Iii. Activation of activity

Now that the application has only one boot entry, how does the other activity start?

There are two ways to start activity, the first is to display the boot, and the other is to start it implicitly.


A.activity Display Start-up

Show startup is the class that you specify to start when you create the intent (intent)

1. Create Intent

2.startActivity (Intent)

Ability to specify classes with the SetClass method

Intent Intent = new Intent () Intent.setclass (Mainactivity.this, Myactivity.class); startactivity (Intent);
or setclassname specifies the class name

Intent Intent = new Intent () Intent.setclassname (Mainactivity.this, "com.yang.study.MyActivity"); StartActivity ( Intent);
It is also possible to construct intent directly when specifying a class

Intent Intent = new Intent (mainactivity.this, Myactivity.class); startactivity (Intent);
These three forms are capable of displaying the activation activity


Tips: What if you want to start the activity of other applications in one application?

The answer is to be able. Only the activity that is being started is set to true in the configuration of the manifest file in the Export property. Then you can start it in other applications, such as the following:

Intent Intent = new Intent () intent.setclassname ("Com.yang.demo", "com.yang.demo.MainActivity"); StartActivity ( Intent);
The first parameter of the Setclassname method is the package name of the application to be started. The second parameter is the class name to start


B.activity Implicit start-up

The implicit intent is to not specify the class to start at startup. Instead, the activity is started by matching the action action with the data

eg. implicit start-up call interface

Intent Intent = new Intent () intent.setaction (Intent.action_call); Intent.setdata (Uri.parse ("tel://18888009988")); StartActivity (Intent);
To start the browser implicitly:

Intent Intent = new Intent () intent.setaction (Intent.action_view); Intent.setdata (Uri.parse ("http://www.baidu.com") );
<span style= "font-family:arial, Helvetica, Sans-serif;" >startactivity (Intent);</span>
The above two examples are to start the system comes with some of the interface, about the system default interface, there are many. To be able to participate in the online information, because the space is limited, here is not listed.


Let's say that our activity is a browser tool. When we use the implicit launch browser above, we also want our activity to be started, how to deal with it?

This time. We just need to set the Intent-filter intent filter in the avtivity manifest file, and when we use the implicit Start browser method above, our custom activity will also be started at the same time.

Examples include the following:

<activity            android:name= "com.yang.study.MyActivity"            android:exported= "true" >            < intent-filter>                <action android:name= "Android.intent.action.VIEW"/>                <data android:scheme= " http "/>                <category android:name=" Android.intent.category.DEFAULT "/>            </intent-filter>        </activity>

Setting the Intent-filter scheme in the manifest file is limited to HTTP, so it is possible to intercept the intent of all actions to start with HTTP Android.intent.action.VIEW data.

When we start the activity implicitly, our own activity is also activated.

Intent Intent = new Intent () intent.setaction (Intent.action_view); Intent.setdata (Uri.parse ("http://www.baidu.com") ); StartActivity (intent);

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvy3nyx3lhbmc=/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">


Iv. Display of activity

Activity is a user to see the interface, then how we define what to see, very easy, in the activity of the Oncreat () method, in which we can set the content we want to display

protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);}
The Setcontentview () method sets the interface that the user wants to display, in which the Ayout layout file to display can be


V. Closure of activity

Acitivty close the Finish () method of calling activity directly to

protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Finish ();}


This article briefly introduces the activity of the start and close, the focus is the display and implicit two start-up mode, the actual use of the frequency is very high, it is necessary to master.



















Android Learning Path--android Four components activity (i)

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.