Android Development Learning--activity

Source: Internet
Author: User

Activity life cycle

#Activity生命周期
# # #void OnCreate ()
* Activity has been created
# # #void OnStart ()
* Activity has been shown on the screen but not getting 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
# # #使用场景
* When the activity is created, the resource needs to be initialized, the resource should be freed when destroyed, or the player app will need to pause automatically when the interface enters the background.

# # #完整生命周期 (entire lifetime)
Oncreate-->onstart-->onresume-->onpause-->onstop-->ondestory

# # #可视生命周期 (Visible lifetime)
Onstart-->onresume-->onpause-->onstop

# # #前台生命周期 (foreground lifetime)
Onresume-->onpause

Create a second activity
* Newly created activity, must be configured in the manifest file, otherwise the system can not be found, the display will be directly error

<activity android:name= "Com.itheima.createactivity.SecondActivity" ></activity>
* As long as the following code, then is the portal activity, will generate a shortcut icon

<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>

<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>

* If the activity contains a package with the same name as the application package, you can omit the

Steps to create an activity:

1. Create class classes to inherit activity
2. Create the layout file as the activity display content
3. Registering activity in the manifest file

#Activity的跳转
>activity jumps need to create intent object, specify the intent object to jump activity by setting parameters

> Implements jumps by setting the package name and class name of the activity, which is called an explicit intent

> Jump by specifying an action, called implicit intent

# # # Explicit 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 ();
Start the system's own dialer app
Intent.setclassname ("Com.android.dialer", "com.android.dialer.DialtactsActivity");
StartActivity (Intent);

# # # Implicit Intent
* Implicit intent to jump to specified activity

  intent Intent = new Intent ();
  //start the system's own dialer app
     intent.setaction (intent.action_dial);
     startactivity (Intent);
* To allow an activity to be started implicitly, 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 specified action (can be customized, can use the system comes with)
* Data specified (what to do)
* Category categories (default category, set-top box, car pc)
* Implicitly intended to start 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
# # # #获取通过setData传递的数据

Gets the intent object that initiated this activity
Intent Intent = Getintent ();
Uri uri = Intent.getdata ();


# # #显式意图和隐式意图的应用场景
* Explicit intent to initiate activity in the same app
* Implicit intent to activate activity in different applications
* If there are multiple activity intent-filter in the system that match your intent at the same time, then a dialog will be displayed, listing all matching activity, which the user chooses to start


#Activity跳转时的数据传递
* When activity is initiated via intent, data can be carried from the intent object to the target activity
Intent Intent = new Intent (this, secondactivity.class);
Intent.putextra ("Malename", malename);
Intent.putextra ("Femalename", femalename);
StartActivity (Intent);
* Remove data from the target activity
Intent Intent = Getintent ();
String malename = Intent.getstringextra ("Malename");
String femalename = Intent.getstringextra ("Femalename");

# #内存不足
* When the memory is low, the system will first kill the process of the background activity, all killed, if the memory is still insufficient, then it will kill the suspended state activity in the process, if not enough, it is possible to kill the foreground process
* If there are multiple background processes, use the least Recently used algorithm (LRU) when selecting the target to kill

# # #Activity任务栈
* During application run, multiple activity may be opened in memory, then all open activity will be saved in the activity task stack
* Stack: LIFO, the most advanced stack, will be the last stack

#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: Browser bookmarks
*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

*singleinstanceThe startup mode is very special, and the activity runs in its own task stack, and only one instance of the task stack exists.
* If you want to make sure that an activity has only one instance in the entire mobile OS, use SingleInstance
* Application scenario: Phone dial interface

#横竖屏的切换

> By default, toggle the screen, destroy the current activity, recreate a new activity
> Shortcut keys Ctrl+f11

* The activity will destroy the rebuild when switching between the screens and the screen in order to read the new layout file
In some special applications, such as the game, do not want the screen switch activity is destroyed re-create
Requirements: Disable the life cycle of the screen switch
1. Write dead on the screen
android:screenorientation= "Landscape"
android:screenorientation= "Portrait"

2. Let the system's environment no longer be 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. Implement the logic of setting data in the newly opened interface

Intent data = new Intent ();
Data.putextra ("Phone", phone);
Sets a result data that is returned to the caller
Setresult (0, data);
Finish ();//close the current activity before returning the data

3. Implement the method in the activity of the opener
Onactivityresult (int requestcode, int resultcode, Intent data)
Retrieving the returned data via data
4. Determine business logic based on request code and result code

Android Development Learning--activity

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.