The Main Function of Activity and its Basic Usage

What is Activity?

The application’s user interface. A container for all controls.

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI withsetContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows (via a theme with windowIsFloating set) or embedded inside of another activity (using ActivityGroup).

To create an Activity class ,do the following step:

Step 1:Create a class,and inherits from Activity. Step 2: Override some inherited methods like onCreate() and onPause(). Step 3:Register each Activity class in the AndroidMainFest.xml. Step 4:Make a layout.xml file,general each Activity class require one layout.xml. Step 5:Add necessary controls into the layout.xml.

The below is an example:

ActivityTest.class:

public class ActivityTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textView1 = (TextView)findViewById(R.id.myTextView);
textView1.setText(“Hello world!!!”);

}

}

AndroidManifest.xml:

<?xml version=”1.0” encoding=”utf-8”?>











mail.xml

<?xml version=”1.0” encoding=”utf-8”?>



Using Control in the ActivityTest:

setContentView(R.layout.main);
TextView textView1 = (TextView)findViewById(R.id.myTextView);
textView1.setText(“Hello world!!!”);

arthinking wechat
欢迎关注itzhai公众号