Day06 activity life cycle and intent usage one, activity's Jump 1. Create a second activity
- You need to configure an activity tag for it in the manifest file
If this child node is in the label, a shortcut icon is created in the system
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
- An application can create multiple shortcut icons on the desktop
Activity name, icon can be different from application name, icon
android:icon="@drawable/ic_launcher"android:label="@string/app_name"
2. Activity's Jump
The activity's jump needs to create the intent object, specifying the intent object's parameters to jump to the activity
A jump is implemented by setting the package name and class name of the activity, which is called an explicit intent
To implement a jump by specifying an action, called an implicit intent
3. Intent 3.1 Display
Jump to another activity under the same item and specify the activity's bytecode directly
Intent intent = new Intent();intent.setClass(this, SecondActivity.class);startActivity(intent);
To jump to activity in another app, you need to specify the app's package name and the activity's class name
Intent intent = new Intent();// 启动系统自带的拨号器应用intent.setClassName("com.android.dialer", "com.android.dialer.DialtactsActivity");startActivity(intent);
3.2 Implicit Intent
Implicit intent to jump to the specified activity
Intent intent = new Intent();// 启动系统自带的拨号器应用intent.setAction(Intent.ACTION_DIAL);startActivity(intent);
To allow an activity to be implicitly launched, you need to set the Intent-filter child node in the activity node of the manifest file
<intent-filter > <action android:name="com.itheima.second"/> <data android:scheme="asd" android:mimeType="aa/bb"/> <category android:name="android.intent.category.DEFAULT"/></intent-filter>
- ACTION Specifies actions (can be customized, can be used with system's own)
- Data specified (what to do)
- Category categories (default category, set-top box, car pc)
- Implicitly intended to start the activity, you need to set the above three properties for intent, and the value must match the activity's definition of three properties in the manifest file
- Both the Intent-filter node and its child nodes can be defined multiple at the same time, and the implicit start-up simply matches any one
Get the data passed through SetData
// 获取启动此Activity的intent对象Intent intent = getIntent();Uri uri = intent.getData();
3.3 Data transfer when the activity jumps
When activity is started through intent, it can carry data to the target activity through the intent object
Intent intent = new Intent(this, SecondActivity.class);intent.putExtra("maleName", maleName);intent.putExtra("femaleName", femaleName);startActivity(intent);
Extracting data from the target activity
Intent intent = getIntent();String maleName = intent.getStringExtra("maleName");String femaleName = intent.getStringExtra("femaleName");
Second, activity life cycle
- void OnCreate ()
- Activity has been created
- void OnStart ()
- Activity has been shown on the screen but has not got the focus
- void Onresume ()
- Activity gets the focus and can interact with the user
- void OnPause ()
- Activity loses focus and can no longer interact with the user, but remains visible
- void OnStop ()
- Activity not visible, into the background
- void OnDestroy ()
- void Onrestart ()
- This method is executed when activity is never visible and becomes visible
1. Usage Scenarios
- When the activity is created, the resource needs to be initialized, the resource is freed when it is destroyed, or the player app needs to be automatically paused when the interface enters the background.
2. Full life cycle (entire lifetime)
- Oncreate–>onstart–>onresume–>onpause–>onstop–>ondestory
3. Visual life cycle (visible lifetime)
- Onstart–>onresume–>onpause–>onstop
4. Foreground life cycle (foreground lifetime)
Iii. four kinds of activation modes of activity
Each app will have an activity task stack that holds the activated activity
Activity start mode, that is, modify the arrangement of the task stack
1. Standard start mode
2. Singletop Single Top mode
- If the activity on the stack at the top of the task stack exists, the activity is not recreated, but the activity already exists is reused. Ensure that the top of the stack is not created repeatedly if it exists
- Application scenario: Bookmarks for browsers
3. Singetask single task stack, only one instance exists in the current task stack
- When the activity is activated, check to see if an instance exists in the task stack, reuse the existing activity if there is an instance, and empty all other activity on the activity. Reuse this existing activity to ensure that only one instance of the entire task stack exists
- Application Scenario: Browser activity
- If the creation of an activity requires a large amount of system resources (CPU, memory), it is generally necessary to configure the activity as the Singletask startup mode
4. SingleInstance Boot Mode
- The activity runs in its own task stack, and only one instance of the task stack exists
- If you want to ensure that only one instance of an activity is present in the entire mobile operating system, use SingleInstance
- Application scenario: Phone dial interface
Four, the life cycle of screen switching
By default, the screen toggles, destroys the current activity, and re-creates a new activity
Shortcut key Ctrl+f11
V. Start activity Get return value
Function: From a interface to open the B interface, B interface closed when the return of a data to a interface
Start activity and get return value
startActivityForResult(intent, 0);
Logic to set up the data in the newly launched interface
Implement methods in starter activity
// 通过data获取返回的数据onActivityResult(int requestCode, int resultCode, Intent data) {}
- Determining the function of the return value by judging the request code and the result code
Android Basics: Day06 activity life cycle and intent usage