在文档android-sdk-windows/docs/offline.html中存放着帮助文件,可以查找各种方法的使用方法
public class Activity extends ApplicationContext { protected void onCreate(Bundle savedInstanceState); protected void onStart(); protected void onRestart(); protected void onResume(); protected void onPause(); protected void onStop(); protected void onDestroy(); }
可以重载这些函数来选择时机执行相应的动作.
这些函数的调用时机可以参考帮助文档
onCreate:
Called when the activity is first created.
This is where you should do all of your normal static set up: create views, bind data to lists, etc.
This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.
Always followed by onStart()
.
onRestart:
Called after your activity has been stopped, prior to it being started again.
Always followed by onStart()
onStart:
Called when the activity is becoming visible to the user.
Followed by onResume()
if the activity comes to the foreground, or onStop()
if it becomes hidden.
onResume:
Called when the activity will start interacting with the user.
At this point your activity is at the top of the activity stack, with user input going to it.
Always followed by onPause()
.
onPause:
Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.
Followed by either onResume()
if the activity returns back to the front, or onStop()
if it becomes invisible to the user.
onStop:
Called when the activity is no longer visible to the user, because another activity has been resumed and is covering this one. This may happen either because a new activity is being started, an existing one is being brought in front of this one, or this one is being destroyed.
Followed by either onRestart()
if this activity is coming back to interact with the user, or onDestroy()
if this activity is going away.
onDestory:
The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called
finish() )on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isfinishing()method.
下面是使用system.out打印的启动顺序输出结果:
应用程序刚刚启动
11-01 11:07:14.964: I/System.out(219): activity02 onCreate
11-01 11:07:15.042: I/System.out(219): activity02 onStart
11-01 11:07:15.052: I/System.out(219): activity02 onResume
启动第二个Activity
11-01 11:07:44.752: I/System.out(219): activity02 onPause
11-01 11:07:44.842: I/System.out(219): OtherActivity onCreate
11-01 11:07:44.872: I/System.out(219): otheractivity onStart
11-01 11:07:44.872: I/System.out(219): otheractivity onResume
11-01 11:07:45.112: I/System.out(219): activity02 onStop
返回到第一个应用程序
11-01 11:07:50.332: I/System.out(219): otheractivity onPause
11-01 11:07:50.391: I/System.out(219): activity02 onRestart
11-01 11:07:50.391: I/System.out(219): activity02 onStart
11-01 11:07:50.391: I/System.out(219): activity02 onResume
11-01 11:07:50.762: I/System.out(219): otheractivity onStop
11-01 11:07:50.762: I/System.out(219): otheractivity onDestroy
要使用system.out输出日志,则需要使用DDMS中的LogCat过滤器,添加System.out过滤器
应用程序的Activity是存储在栈(stack)中的,没启动一个Activity则会被压入到栈中,使用
返回按钮时会弹出栈,如果在Activity中使用finish结束Activity则其会被销毁将不再存在于栈中
如果要使一个Activity具有对话框窗口的样式则只要修改AndroidManifest.xml文件,使它的theme的style风格是Dialog类型就行了
android:theme="@android:style/Theme.Dialog"