Brief Introduction to the activity Startup Process and learning plan of Android applications

Source: Internet
Author: User

In the Android system, activity and service are the core components of applications. They are loosely coupled to form a complete application, this is because the application framework layer provides a complete set of mechanisms to help applications start these activities and services, and provides the Binder Mechanism to help them communicate with each other. In the previous article, the android inter-process communication (IPC) mechanism binder briefly introduced and analyzed the learning plan and the principle of the Android system starting the Custom Service Process (startservice) in the new process, we have systematically introduced the Binder Mechanism and service startup process. In this article, we will briefly introduce the activity startup process and subsequent learning plans.

In the Android system, there are two operations that will trigger activity startup. When a user clicks the application icon, launcher will start the main activity of the Application for us; after the default activity of the application is started, it can call the startactvity interface internally to start a new activity. In this way, each activity can start a new activity internally. Through this chain reaction, the activity is started as needed to complete the functions of the application.

Here, we use a specific example to illustrate how to start the activity of the android application. There are two ways to start an activity: explicit and implicit. Implicit startup can make the coupling between activities more loose. Therefore, here, we only focus on the implicit method for starting the activity.

First, create an application project directory activity under the packages/experimental directory of the android source code project. For more information about how to obtain the android source code project, see download, compile, and install the latest Android source code on Ubuntu. For more information about how to create an application project in the android source code project, for details, refer to the article on Testing hardware services at the application frameworks layer for Android built-in Java applications on Ubuntu. Here, the project name is activity, which defines a package with the path shy. Luo. activity. The source code of this example is implemented here. Next, we will introduce the files in the package one by one.

The default activity of the application is defined in the src/shy/LUO/activity/mainactivity. Java file:

package shy.luo.activity;import shy.luo.activity.R;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity  implements OnClickListener {private final static String LOG_TAG = "shy.luo.activity.MainActivity";private Button startButton = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);startButton = (Button)findViewById(R.id.button_start);startButton.setOnClickListener(this);Log.i(LOG_TAG, "Main Activity Created.");}@Overridepublic void onClick(View v) {if(v.equals(startButton)) {Intent intent = new Intent("shy.luo.activity.subactivity");startActivity(intent);}}}

Its implementation is very simple. When you click a button above it, it starts the actvity named "Shy. Luo. activity. subactivity.

The actvity named "Shy. Luo. activity. subactivity" is implemented in the src/shy/LUO/activity/subactivity. Java file:

package shy.luo.activity;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SubActivity extends Activity implements OnClickListener {private final static String LOG_TAG = "shy.luo.activity.SubActivity";private Button finishButton = null;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.sub);finishButton = (Button)findViewById(R.id.button_finish);finishButton.setOnClickListener(this);Log.i(LOG_TAG, "Sub Activity Created.");}@Overridepublic void onClick(View v) {if(v.equals(finishButton)) {finish();}}}

Its implementation is also very simple. When you click the above button, you end yourself and return to the previous activity.

Here we can see that the very core of the android application architecture is that mainactivity does not need to know the existence of subactivity, that is, it does not directly have subactivity interfaces, however, it can use a string to tell the application framework layer what is the name of the activity to be started, and other things will be done at the application framework layer. Of course, the application framework layer finds the corresponding activity based on the string and starts it. In this way, the activity coupling in Android applications is loose, which leads to a high degree of attention of Android applications and facilitates the maintenance and update of future programs, this is very important for large client software.

Of course, the application framework can find the corresponding activity by name, which requires the application itself to work together. This is achieved through the application configuration file androidmanifest. xml:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="shy.luo.activity"android:versionCode="1"android:versionName="1.0"><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".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=".SubActivity"          android:label="@string/sub_activity"><intent-filter><action android:name="shy.luo.activity.subactivity"/><category android:name="android.intent.category.DEFAULT"/></intent-filter></activity></application></manifest>

From this configuration file, we can see that mainactivity is configured as the default activity of the application, that is, when you click the activity application icon on the mobile phone screen, launcher will start the mainactivity by default:

<activity android:name=".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>

This configuration file also associates the name "Shy. Luo. activity. subactivity" with subactivity. Therefore, the application framework layer can find it by name:

<activity android:name=".SubActivity"  android:label="@string/sub_activity"><intent-filter><action android:name="shy.luo.activity.subactivity"/><category android:name="android.intent.category.DEFAULT"/></intent-filter></activity>

The application configuration file and string file are listed below.

The interface configuration file is in the Res/layout directory. The main. xml file corresponds to the mainactivity interface:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"     android:gravity="center">        <Button             android:id="@+id/button_start"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:gravity="center"            android:text="@string/start" >        </Button></LinearLayout>

The sub. xml corresponds to the subactivity interface:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"     android:gravity="center">        <Button             android:id="@+id/button_finish"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:gravity="center"            android:text="@string/finish" >        </Button></LinearLayout>

The string file is located in the Res/values/strings. xml file:

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Activity</string>    <string name="sub_activity">Sub Activity</string>    <string name="start">Start sub-activity</string>    <string name="finish">Finish activity</string></resources>

Finally, we need to place a compilation script file Android. mk in the project directory:

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_SRC_FILES := $(call all-subdir-java-files)LOCAL_PACKAGE_NAME := Activityinclude $(BUILD_PACKAGE)

In this way, the source code implementation of the entire example is complete, and the compilation will be completed. For details about how to separately compile the android source code project module and how to package system. IMG, refer to the article on how to separately compile the android source code module.
Run the following command to compile and package:

USER-NAME@MACHINE-NAME:~/Android$ mmm packages/experimental/Activity  USER-NAME@MACHINE-NAME:~/Android$ make snod 

In this way, the packaged Android system image file system. IMG contains the previously created activity application.
Next, we will run the simulator to run our example. For details about how to run the simulator in the android source code project, refer to download, compile, and install the latest Android source code on Ubuntu.
Run the following command to start the simulator:

USER-NAME@MACHINE-NAME:~/Android$ emulator  

When the simulator starts, you can see the activity application icon on the screen:

Click the application icon of activity to start mainactivity:

Click the start sub-activity button above. mainactivity starts subactivity through the startactivity interface:

Intent intent = new Intent("shy.luo.activity.subactivity");startActivity(intent);

As shown in:

Whether you start an activity by clicking the application icon or calling the startactivity interface inside the activity to start a new activity, you must use the activitymanagerservice service process at the application framework layer. In the previous article on how the Android system starts the Custom Service Process (startservice) in the new process, we have seen that the service is also started by the activitymanagerservice process. In the framework layer of Android applications, activitymanagerservice is a very important interface. It not only starts activity and service, but also manages activity and service.

The activitymanagerservice In the Android Application Framework layer starts the activity process as follows:

In this figure, activitymanagerservice and activitystack are in the same process, while applicationthread and activitythread are in another process. Among them, activitymanagerservice is responsible for managing the lifecycle of the activity, and activitymanagerservice also uses activitystack to place all the activities in a stack in the order of going first and foremost. for each application, each activitythread contains an applicationthread instance, which is a binder object that communicates with other processes.

The following briefly introduces the startup process:

Step 1. whether you start an activity through launcher or call the startactivity interface inside the activity to start a new activity, it enters the activitymanagerservice process through communication between the binder processes and calls activitymanagerservice. startactivity interface;

Step 2. activitymanagerservice calls activitystack. startactivitymaywait to prepare information about the activity to be started;

Step 3. activitystack notifies applicationthread to start and schedule the activity. Here applicationthread indicates that activitymanagerservice is called. startactivity interface process, for the scenario of clicking the application icon, this process is the launcher, and for the scenario of calling startactivity within the activity, this process is the process of the activity;

Step 4. applicationthread does not execute the actual Startup operation. It calls the activitymanagerservice. activitypaused interface to enter the activitymanagerservice process to see if a new process needs to be created to start the activity;

Step 5. when you click the application icon to start the activity, activitymanagerservice calls startprocesslocked to create a new process, this step does not need to be executed to start a new activity by calling startactivity within the activity, because the new activity is started in the process where the original activity is located;

Step 6. activitymanagerservic calls the applicationthread. schedulelaunchactivity interface to notify the corresponding process to start the activity;

Step 7. applicationthread forwards the activity Startup operation to activitythread. activitythread imports the activity class through classloader and starts it.

In this way, the activity Startup Process of the Android Application is briefly introduced here. In the next two articles, we will refer to the two startup scenarios of the activity, go deep into the source code of the application framework layer and analyze their startup process step by step:

1. Source Code Analysis of the android application startup process;

2. Source Code Analysis of the startup activity process (startactivity) in the Android Application.

Lao Luo's Sina Weibo: http://weibo.com/shengyangluo. welcome to the attention!

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.