How Android starts the activity

Source: Internet
Author: User


Activity is launched in two ways, display and implicit, by specifying the package name and class name of the activity when the activity is started through intent.
The implicit way is to specify only the action or action and data properties when initializing intent. The next step is to summarize the ways and considerations for starting and implicitly booting.


1. Activity display start: Start with the package name (whether it's activating your app's activity or activating other apps)

1.1 How to ComponentName objects
ComponentName cn = New ComponentName ("Com.example.ipc_activity",
"Com.example.ipc_activity. Activitya ");
Intent Intent = new Intent ();
Intent.setcomponent (CN);
StartActivity (Intent);
1.2 Specifying the package name when initializing intent
Intent Intent = new Intent ("Android.intent.action.MAIN");
Intent.setclassname ("Com.example.ipc_activity",
"Com.example.ipc_activity. Activitya ");
StartActivity (Intent);


2, Activity's implicit start: by specifying the action or action and the Data property to start activity, if there are multiple activity to meet this launch condition, the system will let the user choose to launch the app.

2.1 Specifies the Action property only for the activated activity. Specify the Action property for the activity tag to be started in the Androidmanifest.xml file. As follows:
Properties defined in the Activity tab:
<activity
Android:name= "Com.example.ipc_activity. Activityb "
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Dxd.ipcdemo.activity.DEMO"/>
<category android:name= "Android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
where <category android:name= "Android.intent.category.DEFAULT"/> is the property that is required for an implicit start activity.
Implicit start-Up code:
Intent Intent = new Intent ("Dxd.ipcdemo.activity.DEMO");
StartActivity (Intent);

2.2 Specify the action and data properties for the activated activity. As follows:
<activity
Android:name= "Com.example.ipc_activity. Activityb "
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Dxd.ipcdemo.activity.DEMO"/>
<data android:scheme= "Info"/>
<category android:name= "Android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
The same <category android:name= "Android.intent.category.DEFAULT"/> is also required.
Implicit start-Up code:
Intent Intent = new Intent ("Dxd.ipcdemo.activity.DXD", Uri.parse ("info://"));
StartActivity (Intent);
In comparison, the following two conclusions can be summed up:
A: An implicit start activity must specify the default property for the target activity
B: If the <data> attribute is specified, the data attribute must be added at startup, otherwise the error will be given.

3, Android activation activity can be used in the previous two ways, whether it is the activity of the application or the activity of another program can be resolved. But if it's started,
Your own application of activity, there is no need to be so troublesome, directly through the designation of Activity.class. Here's how:
Intent Intent = new Intent (activitya.this, Activityb.class);
StartActivity (Intent);

4. Launch APK with package name
Intent Intent = Getpackagemanager (). getlaunchintentforpackage
("Com.example.ipc_activityb");
if (intent! = NULL)
StartActivity (Intent);

Add 1: This is the way Android launches activity, in addition to start activity there are 4 startup modes, in the Activity tab to configure Launch_mode.
1. Standard
mode startup mode, activity is created each time the activity is activated and placed in the task stack.

2. Singletop
If there is an instance of the activity at the top of the task, reusing the instance will create a new instance and put it on top of the stack, even if the activity already exists in the stack
Example, an instance is created as long as it is not on top of the stack.

3. Singletask
If an instance of the activity is already in the stack, the instance is reused (Onnewintent () of the instance is invoked). When reused, the instance returns to the top of the stack, so the
The instance will be removed from the stack. If the instance does not exist in the stack, a new instance is created to be put into the stack.


4. SingleInstance

Create the activity instance in a new stack and have multiple apps share the activity instance in the stack. Once an instance of the activity of the schema is present in a stack, any
When the app is reactivated, the instance in the stack is reused, and the effect is equivalent to multiple applications sharing an app, regardless of who activates the activity and enters the same
In use.

Additional 2: Sometimes the project requires such a requirement to transparently load activity. For example, start Activityb in Activitya, ask Activityb not to show, but to execute ACTIVITYB
Code, you need to specify the style for the ACTIVITYB
SetTheme (Android. R.style.theme_nodisplay); You can also specify the style property in the Activity's tab android:theme= "@android: Style/theme.nodisplay".

How Android starts the activity

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.