current position:Home>Start method of activity

Start method of activity

2022-01-26 23:30:13 Chen Yuyi

stay Android in , The jump of the interface is usually by starting different Activity To achieve , So let's talk about that Activity Starting method of .

Explicit call

Explicit call , Literally ” Obvious call “, We can clearly know what we are about to start in the calling method Activity, The specific method of displaying the call is as follows :

val intent = Intent(this,
//  amount to Java Medium  SecondActivity.class
 Copy code 

We need to build a Intent object , The first parameter is passed in this At present Activity The context of , The second object is passed in As the target Activity, So our intention is obvious , That is, we want to jump to SecondActivity This interface , We just need to call startActivity This function can achieve our goal .

Implicit call

Implicit calls are the opposite of explicit calls , It doesn't specify which... To jump to Activity, But through action, category [ˈkætəɡəri] and data These three filtering information are matched by the system Activity.

by Activity Set filtering information

If we want to start an implicit call Activity, We have to work for this first Activity Set filtering information , Otherwise, this cannot be started through implicit calls Activity Of . Filter information in AndroidMainfest.xml Register in the file Activity Set when , By means of Configuration under the label The content of , You can specify the current Activity Capable of responding to action,category and data.

        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
            android:scheme="bjhlliveapp" />
 Copy code 

Start when using implicit call Activity when , Need to match at the same time action,category and data, Otherwise the match fails . One Under the action,category and data There can be multiple , One activity There can be multiple label , As long as the information in any one of them is successfully matched, the matching can be successful .

The following describes the matching rules of various information in detail .


action Is a string ( Case sensitive ) , The system has predefined some for us action, Such as android.intent.action.MAIN etc. , At the same time, we can also define ourselves action, One There can be more than one action.

When we use implicit calls Intent Must specify action, When action and activity Of Any one of action identical ( The value of the string is the same ) when , The match is successful .


category and action The same is a string , At the same time, there are defined in the system category, We can define it ourselves category, But if you want one activity Support implicit call , Then it must be in It is specified in “android.intent.category.DEFAULT” This category.

category The matching rules of action Different , When implicitly called Intent Must specify action, But there can be no category, But if you specify category ( It can be one or more ) , Then all specified category And Medium category identical , Otherwise the match fails .

In popular terms ,category You may not specify ( If it is not specified, it can be matched successfully ) , But once you specify , You have to make sure you specify these category All with what you're about to start activity One of them Medium category identical .

Why not specify, but can match successfully ? Because I said if activity Support implicit call , Then there must be “android.intent.category.DEFAULT” This category, The system is calling startActivity perhaps startActivityForResult It defaults to Intent add “android.intent. category.DEFAULT” This category, So you can match success .


data The grammatical structure of is as follows :

<data android:scheme="string"
      android:mimeType="string" />
 Copy code 

data It's made up of two parts ,mimeType and URI.mimeType Media type , such as image/jpeg、audio/mpeg4-generic and video/* etc. , Can represent a picture 、 Text 、 Different media formats such as video , and URI It contains more data , Here is URI Structure :

 Copy code 

for example :

 Copy code 

URI The meaning of each data in is as follows :

  • Scheme:URI The pattern of , such as http、file、content etc. , If URI Not specified in scheme, So the whole URI Invalid other parameters for , It also means that URI It's invalid .
  • Host:URI The host name , such as, If host Not specified , So the whole URI Other parameters in are invalid , It also means that URI It's invalid .
  • Port:URI Port number in , such as 80, Only when the URI Specified in scheme and host Parameter time port Parameters make sense .
  • path、pathPattern and pathPrefix: These three parameters represent path information , among path Represents complete path information ;pathPattern It also represents the complete path information , But it can contain wildcards “”,“” Express 0 Any character or characters , It should be noted that , Due to the specification of regular expressions , If you want to represent a real string , that “*” To write “*”, “\” To write “ \\”;pathPrefix Prefix information representing the path .

data The matching rules are complex , But in general Intent Medium data It has to be with In the same , If There is no , be Intent either , If There is , be Intent Must have tangent and In the same way .

Calling method

val intent = Intent("android.intent.action.CPW")
// intent  Constructor specifies  action
// addCategory  Method specification category
intent.setDataAndType(Uri.parse("file://abc"), "text/plain")
// setDataAndType  Method specification data, Parameter is  URI  and  mimeType
 Copy code 

That's about Activity Start the whole content of the method !

Welcome WeChat search to pay attention to official account. :Android No worries
Learn more together Android knowledge

copyright notice
author[Chen Yuyi],Please bring the original link to reprint, thank you.

Random recommended