Android-based Activity and androidactivity

Source: Internet
Author: User

Android-based Activity and androidactivity

1. What is Activity?

Activity is one of the four main components of Android, and Activity is the most important component.

Activity provides users with a window for information interaction.

Ii. How to create an Activity

1. Create a subclass that inherits the Activity

1 public class MainActivity extends Activity {2 3 @ Override4 protected void onCreate (Bundle savedInstanceState) {5 super. onCreate (savedInstanceState); 6 setContentView (R. layout. activity_main); // load layout 7} 8}

2. Configure the new Activity class in the AndroidManifest. xml file of the function list.

 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3     package="com.example.newactivity" 4     android:versionCode="1" 5     android:versionName="1.0" > 6  7     <uses-sdk 8         android:minSdkVersion="8" 9         android:targetSdkVersion="17" />10 11     <application12         android:allowBackup="true"13         android:icon="@drawable/ic_launcher"14         android:label="@string/app_name"15         android:theme="@style/AppTheme" >16         <activity17             android:name="com.example.newactivity.MainActivity"18             android:label="@string/app_name" >19             <intent-filter>20                 <action android:name="android.intent.action.MAIN" />21 22                 <category android:name="android.intent.category.LAUNCHER" />23             </intent-filter>24         </activity>25     </application>26 27 </manifest>

Iii. Task Stack

Task Stack origin:

    An Android application consists of one or more Activity components or other components. In order to record the activities enabled by the user and the order in which these activities are enabled.

  Features of the Task Stack:

1. When an Android Application is enabled, the Android operating system assigns a task stack to it. When all the activities exit, the task stack is cleared;

2. The task stack id is an auto-increment int data type. You can use the getTaskId () method to obtain the task stack id;

3. Multiple Task stacks exist in the Android operating system. An Android application can have one or more task stacks;

4. By default, if an application is disabled, the task stack assigned to the application by the Android operating system is cleared, but the process of the application is retained;

4. Activity Startup Mode

  Activity startup modes include standard (default), singleTop, singleTask, and singleInstance.

1. standard Mode

In this startup mode, every time the Activity is activated (startActivity), a new Activity instance is created and placed on the top of the stack of the task stack.

2. singleTop

In this startup mode, ifWhen singleTop is at the top of the stackYou do not need to create a new Activity and call the onNewIntent () method (the onNewIntent () method is not called when singleTop is enabled for the first time ),

Otherwise, you need to re-create the Activity instance, but will not call onNewIntent ();

3. singleTask (commonly used)

In this startup mode, if a singleTask does not need to create a new Activity at the top of the stack, the onNewIntent () method is called;

If a singleTask is not on the top of the stack and has a singleTask in the stack, you do not need to create a new Activity. Instead, you can call the onNewIntent () method, other Activity instances on singleTask will be deleted.

4. singleInstance

In this startup mode, if the Android program of the Activity has been activated or has been activatedSeparateIn the Task Stack

If other Android programs are started, the Activity will be re-activated and the onNewIntent () method in the Activity will be called;

  Another app opens the Activity program for demonstration

  

 1 public class App02Activity extends Activity { 2  3     @Override 4     protected void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6         setContentView(R.layout.activity_app02); 7         ((Button) findViewById(R.id.access_bt)).setOnClickListener(new OnClickListener() { 8             @Override 9             public void onClick(View v) {10                 Intent intent = new Intent("com.shxt.activity.singleinstance");11                 startActivity(intent);12             }13         });14     }15 }

 

1 <! -- AndroidManifest in the Activity. 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>

 

5. Activity Lifecycle

Full Lifecycle of an Activity:

Void onCreate ()

Void onStart ()

Void onResume ()

Void onPause ()

Void onStop ()

Void onDestroy ()

1. A complete life cycle starts when onCreate () is called for the first time until the onDestory () method is called.

The Activity completes initialization in onCreate () and releases all system resources in onDestroy.

2. Visual life cycle: the life cycle starts from onStart () until the onStop () method is called. In this case, the method can be called multiple times as the application is visible to the user

3. Foreground life cycle: it is called by onReusme () to the onPause () method. The Activity is active and can interact with users.

Related Article

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.