Android Basics Activity

Source: Internet
Author: User

I. What IS activity

Activity is one of the four main components of Android, and activity is a priority in components.

Activity is a window for users to interact with information.

Second, how to create activity

1. Create a new subclass that inherits activity

1  Public classMainactivityextendsActivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6Setcontentview (R.layout.activity_main);//Loading Layouts7     }8}

2. The new activity class needs to be configured in the function manifest Androidmanifest.xml file

1<?xml version= "1.0" encoding= "Utf-8"?>2<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"3      Package= "Com.example.newactivity"4Android:versioncode= "1"5Android:versionname= "1.0" >6 7<uses-SDK8Android:minsdkversion= "8"9Android:targetsdkversion= "/>"Ten  One<Application AAndroid:allowbackup= "true" -android:icon= "@drawable/ic_launcher" -Android:label= "@string/app_name" theAndroid:theme= "@style/apptheme" > -         <activity android:name= "com.example.newactivity.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>  -</application> -  -</manifest>

Three, the task stack

The origin of the task stack:

    An Android application has one or more activity components or other components, in order to record what activity the user is opening and the order in which the activity is opened.

  Features of the task stack:

1. When an Android application is turned on, the Android operating system assigns it a task stack, and when all activity exits, the task stack is emptied;

2, the task stack ID is a self-growing int data type, you can get the ID of the task stack through the GetTaskID () method;

3, there are multiple task stacks in the Android operating system, an Android application can have one or more task stacks;

4. By default, shutting down an application clears the task stack assigned to the application by the Android operating system, but the application process is retained;

Iv. activation mode of activity

  There are four modes in the activity's startup mode, namely standard (default), Singletop, Singletask, singleinstance

1.standard mode

In this startup mode, each time the activity is activated (startactivity) A new activity instance is created and placed on the stack top of the task stack

2.singleTop

In this startup mode, if Singletop is at the top of the stack, you do not need to create a new activity, call the Onnewintent () method (the first time enable Singletop does not call the Onnewintent () method),

Otherwise you will recreate the activity instance, but will not call Onnewintent ();

3.singleTask (Common)

In this startup mode, if Singletask is at the top of the stack, you do not need to create a new activity, call the Onnewintent () method;

If Singletask is not at the top of the stack, and there is singletask in the task stack, there is no need to recreate the new activity, but instead call the Onnewintent () method. and other activity instances on top of singletask will be deleted

4.singleInstance

In this startup mode, if the activity's Android program is activated or activated, it will exist in a separate task stack

If the other Android program starts, the activity is reactivated and the Onnewintent () method in the activity is called;

  Another app opens the Activity program demo

  

1  Public classApp02activityextendsActivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (R.LAYOUT.ACTIVITY_APP02);7(Button) Findviewbyid (R.ID.ACCESS_BT)). Setonclicklistener (NewOnclicklistener () {8 @Override9              Public voidOnClick (View v) {TenIntent Intent =NewIntent ("com.shxt.activity.singleinstance"); One startactivity (intent); A             } -         }); -     } the}

1    <!--the activity Androidmanifest.xml-- 2         <activity  3             Android:name= ". Singleinstanceactivity " 4             android:launchmode=" SingleInstance "> 5              <intent-filter > 6                 <action android:name= "com.shxt.activity.singleinstance"/> 7 8                 <category android:name= "Android.intent.category.DEFAULT"/> 9             </intent-filter>10          </activity>

V. Activity life cycle

The full life cycle of an activity:

void OnCreate ()

void OnStart ()

void Onresume ()

void OnPause ()

void OnStop ()

void OnDestroy ()

1. A complete life cycle begins with the first call to OnCreate () until the end of the call to the Ondestory () method.

Activity completes initialization in OnCreate () and frees all system resources in OnDestroy ().

2. Visual life cycle: The life cycle is initiated by the OnStart () call until the end of the call to the OnStop () method. Between this, the method can be called multiple times as the application is visible to the user

3. Foreground life cycle: is called by Onreusme () to the OnPause () method. Activity is activated between this and can interact with the user.

Android Basics 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.