Page jumps and data transfer

Source: Internet
Author: User

#创建第二个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"

#Activity的跳转

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 Implicit Intent # # #显式意图

    • 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);

# # #隐式意图

  •   Intent Intent = new Intent ();   Start the system's own dialer application  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 at the same time, and only one match can be used when the implicit startup #获取通过setData传递的数据

       Intent intent = getIntent(); Uri uri = intent.getData();

# # #显式意图和隐式意图的应用场景

    • 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

#Activity跳转时的数据传递

    • 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生命周期

# # #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 ()

    • Activity is destroyed.

# # #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

#Activity的四种启动模式

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

# #横竖屏切换的生命周期

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

    1. Screen Write dead android:screenorientation= "Landscape" android:screenorientation= "Portrait "

    2. Let the system's environment no longer go sensitive to the screen switch.

       android:configChanges="orientation|screenSize|keyboardHidden"

#掌握开启activity获取返回值

# # #从A界面打开B界面, B when the interface is closed, return a data to the A interface

Steps:

    1. Activate activity and get return value

       startActivityForResult(intent, 0);
    2. Logic to set up the data in the newly opened interface

       Intent data = new Intent(); data.putExtra("phone", phone); //设置一个结果数据,数据会返回给调用者 setResult(0, data); finish();//关闭掉当前的activity,才会返回数据
    3. Implement method Onactivityresult (int requestcode, int resultcode, Intent data) in the activity of the opener to get the returned information via data

    4. Determining business logic based on request code and result code

Page jumps and data transfer

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.