An Activity is an application component that provides a screen with users can interact in order to do something, such as dial the phone, take a photo, view a map, or send an email etc. Each activity is given a window in which to draw its user interface.
How to create an Activity:
To create an activity, you must create a subclass of Activity.
onCreate():
You must implement this method. The system calls this when creating your activity.
onPause():
The system calls this method as the first indication that the user is leaving your activity.
Implementing a user interface:
- The user interface for an activity is provided by a hierarchy of views-objects derived from the View class.
- The most common way to define a layout using views is with XML layout file saved in your application resources.
- You can set the layout as the UI for your activity with setContentView(), passing the resource ID for the layout.
Declaring the activity in the manifest:
You must declare your activity in the manifest file in order for it to be accessible to the system.
<manifest ... >
<application ... >
<activity android:name=".HelloActivity" />
...
</application ... >
...</manifest >
<application ... >
<activity android:name=".HelloActivity" />
...
</application ... >
...</manifest >
Managing the Activity LifeCycle:
We can control and manage the resource through the life cycle methods of activity.
There are 7 life cycle methods of android.app.Activity class. They are as follows:
onCreate() –called when activity is first created.
onStart() –called when activity is becoming visible to the user.
onResume() –called when activity will start interacting with the user.
onPause() –called when activity is not visible to the user.
onStop() –called when activity is no longer visible to the user.
onRestart() –called after your activity is stopped, prior to start.
onDestroy() –called before the activity is destroyed.
package com.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class HelloActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(), "onStart() invoked", 2000).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(), "onResume() invoked", 2000).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(), "onPause() invoked", 2000).show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(getApplicationContext(), "onRestart() invoked", 2000).show();
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(), "onStop() invoked", 2000).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(), "onDestroy() invoked", 2000).show();
}
}
0 comments:
Post a Comment