Summary of Android Activities

1. Activities

explicit intent

Intent(Context packageContext, Class<?> cls);

implicit intent

Add a response to the activity: Add the intent-filter tag to an Activity in the AndroidManifest, and specify the content of action and category .
Another constructor corresponding to Intent:

Intent(String action)//action即对应name属性
此外,Intent还有另外几个常用的构造函数
Intent(String action, Uri uri);
Intent(String action, Uri uri, Context packageContext, Class<?> cls)
URI主要是结合程序之间的数据共享ContentProvider
  • More usage
    of Intent Specify action for Intent as built-in action of Android system, such as Intent.ACTION_VIEW
    , Intent.ACTION_DIAL , etc., then parse a string into a Uri object through Uri.parse(), and then call setData() The method passes in a Uri object.
    Correspondingly, we can also configure a < data > tag in the < intent-filter > tag to more precisely specify what type of data the current activity can respond to. The following contents can be configured in the < data > tag:
android::scheme 用于指定数据的协议部分,如http
android::host 用于指定数据的主机名部分,如www.baidu.com
android::port 用于指定数据的端口部分, 一般紧随在主机名之后
android::path 用于指定主机名和端口之后的部分,如一段网址中跟在域名之后的内容
android::mimeTyoe 用于指定可以处理的数据类型,允许使用通配符的方式进行指定

Return data to previous activity

The startAcitivityForResult() method in Activity accepts two parameters, an Intent and a request code. This method expects to return a result to the previous activity when the activity is destroyed.

The activity that expects to return the result builds an Intent (only for passing data, without any specified "intent") before finishing, puts the data that it wants to return into the Intent and then calls setResult() to return it to the previous activity.

setResult(int ResultCode, Intent intent);
//这里的ResultCode一般只是用RESULT_OK/RESULT_CANCELED

When the activity is destroyed, the onACtivityResult() method of the previous activity will be called back.

void onActivityResult(int requestCode, int resulrCode, Intent data)

This method has three parameters, the first parameter requestCode, which is the request code passed in when the activity is started, the second parameter resultCode, which is the processing result passed in when we return the data, and the third parameter data, which is the data returned by the carrier Intent.

Since it is possible to call the startActivityForResult() method in an activity to start many different activities, the data returned by each activity will be called back to the onActivityResult() method, so firstly, you need to check the value of requestCode to determine the source of the data, and then Whether the processing result is successful is judged by the value of resultCode.

Activity startup mode

standard

Standard is the default startup mode for activities. Whenever a new Activity is started, the system does not care whether the activity already exists in the back stack, and a new instance of the activity is created each time it is started.

singleTop

When starting an activity, if it is found that the top of the return stack is already the activity, it is considered that it can be used directly, and a new activity instance will not be created.

singleTask

When the startup mode of the activity is specified as singleTask, the system will first check whether there is an instance of the activity in the return stack every time the activity is started. Activities are all popped off the stack, and if not found, a new activity instance is created.

singleInstance

An activity specified as singleInstance mode enables a new back stack to manage the activity (different programs can share instances of this activity). (This is more complicated and I will not introduce Orz here. >.<)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325564523&siteId=291194637