Analysis of Android's method of creating an Activity

  • 2021-07-01 08:15:36
  • OfStack

This article illustrates how Android creates an Activity. Share it for your reference, as follows:

To create a new Activity, you need to inherit the Activity class, define UI, and implement functionality. The basic framework code for the new Activity is as follows:


package com.paad.myapplication;
import android.app.Activity;
import android.os.Bundle;
public class MyActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
}
}

The basic Activity class represents an empty screen, which is of little use, so the first thing to do is to fill UI with Views and layouts.

UI of Activity is created by Views. Views is an UI control that displays data and provides user interaction. Android provides a number of layout classes, called View Groups, which can accommodate multiple View to help you design complex UI.

View and View Groups were described earlier, involving using and creating custom Views and layouts.

To specify UI for Activity, call the setContentView method in the onCreate method of Activity.

In this code snippet, a simple instance of MyView is used as the UI of Activity:


@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
MyView myView = new MyView(this);
setContentView(myView);
}

In most cases, you want to use the more complex UI design. You can use View Groups to create a layout in your code, or you can use the convenience of standard Android to pass an externally defined resource ID of layout, as shown in the following code snippet:


@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
}

In order to use an Activity in your application, you need to register it in manifest. Add a new activity tag to the application node; activity contains metadata such as label, icon, permissions and themes. Activity without the corresponding activity tag cannot be started.

The following XML fragment shows how to add a node to the MyActivity class I just created:


<activity android:label="@string/app_name"
android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

In the activity tab, you can add intent-filter nodes to indicate which intent your Activity listens to and responds to. Each Intent Filter can define one or more action and categories. Intent and Intent Filter are described in detail in Chapter 5, but they are of little value to an Activity as the primary startup program. It must contain an Intent Filter to listen for MAIN actions and LAUNCHER types, as shown in the following highlighted code snippet:


<activity android:label="@string/app_name"
android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

For more readers interested in Android related content, please check the topics on this site: "Summary of activity Operation Skills for Android Programming", "Summary of Android Communication Methods", "Summary of Android Debugging Skills and Common Problem Solutions", "Introduction and Advanced Tutorial of Android Development", "Summary of Android Basic Component Usage", "Summary of layout Layout Skills" and "Summary of Android Control Usage"

I hope this article is helpful to everyone's Android programming.


Related articles: