Create a second activity (master)
- 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"
Activity's Jump (master)
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
Explicit Intent (mastering)
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);
Implicit Intent (Mastering)
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 data passed through SetData (master)
//获取启动此Activity的intent对象 Intent intent = getIntent(); Uri uri = intent.getData();
Application scenarios for explicit intent and implicit intent (mastering)
- Explicit intent to start activity in the same app
- Implicitly intended to initiate activity in different applications
- If there are multiple activity intent-filter in the system that match your intent at the same time, a dialog box is displayed, listing all matching activity, which the user chooses to start
Data transfer at activity jump (master)
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");
Activity life cycle (mastering) 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
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.
Full life cycle (entire lifetime)
Oncreate-->onstart-->onresume-->onpause-->onstop-->ondestory
Visual life cycle (visible lifetime)
Onstart-->onresume-->onpause-->onstop
Foreground life cycle (foreground lifetime)
Onresume-->onpause
Four startup modes of activity (master)
Each app will have an activity task stack that holds the activated activity
Activity start mode, modify the arrangement of the task stack
- Standard boot Mode
- Singletop Single Top mode
- If the activity on the stack at the top of the task stack exists, it will not recreate the activity, but instead reuse the existing activity. Ensure that the top of the stack is not created repeatedly if it exists.
- Application scenario: Bookmarks for browsers
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 already existing activity. Ensure that only one instance of the entire task stack exists
- Application Scenario: Browser activity
- If an activity is created that requires a large amount of system resources (CPU, memory), this activity is typically configured as a Singletask startup mode. WebKit Kernel C code
SingleInstance startup mode is very special, the activity will run in its own task stack, and there is only one instance of the task stack exists
- If you want to make sure that an activity has only one instance of the entire mobile operating system, use SingleInstance
- Application scenario: Phone dial interface
Life cycle of screen switching (familiar)
By default, the screen toggles, destroys the current activity, and re-creates a new activity
Shortcut key Ctrl+f11
In some special applications, such as the game, do not want to switch between the screen and the activity is destroyed re-create the need: Disable the screen switch life cycle
Screen write dead android:screenorientation= "Landscape" android:screenorientation= "Portrait"
Let the system's environment no longer go sensitive to the screen switch.
android:configChanges="orientation|screenSize|keyboardHidden"
Master open Activity Get return value (Master) from a interface to open the B interface, B interface is closed, return a data to the A interface step:
Activate activity and get return value
startActivityForResult(intent, 0);
Logic to set up the data in the newly opened interface
Intent data = new Intent();data.putExtra("phone", phone);//设置一个结果数据,数据会返回给调用者setResult(0, data);finish();//关闭掉当前的activity,才会返回数据
Implement the method in the activity of the opener
//通过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 Summary (vi)