官网文档是这样的:
Method | Description | Killable? | Next | ||
---|---|---|---|---|---|
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(). | No | onStart() | ||
onRestart() | Called after your activity has been stopped, prior to it being started again. Always followed by onStart() | No | 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. | No | onResume() or onStop() | ||
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(). | No | 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. | Pre-HONEYCOMB | onResume() oronStop() | ||
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 | Yes | onRestart() oronDestroy() | ||
onDestroy() | 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. | Yes | nothing |
onCreate()://一般先初始控件、 绑定监听器、绑使用布局文件XML
当一个新的Activity创建时自动调用该函数。
onRestart()://假如Activity已经被销毁,则不会调用该函数,会调用onCreate()
从一个Activity跳到另外一个Activity时,假设该Activity还没有被销毁,则会调用该函数。
onStart():
当Activity能被我们看到的时候就会调用。
onResume():
当获得用户焦点的时候就会调用。
onPause()://应该保存该Activity的数据,以便返回的时候重新绘制该Activity
当一个Activity启动另外一个Activity时就会调用该Activity的onPause()。
onStop()://若弹出的是对话框,对话框不能完全把该Activity遮挡住,故不会调用该Activity的OnStop()
当该Activity完全被遮挡住的时候就会调用该Activity的onStop()。
onDestroy();//类似析构函数
当Activity被销毁的时候会自动调用,一般由系统自动回收,也可以手动调用,调用方式是在Activity中调用finish()
android中Activity是采用栈的形式来储存的。
在开发过程中要注重用户体验,比如应用程序时用户调用了拨号程序,要注意保存相关数据,以便当用户按返回键的时候能够重新绘制出该应用程序的原貌。让用户觉得拨号程序跟应用程序时同一个程序。
就像栈一样,拨号程序入栈,当出栈后要能看到之前的内容。
用户所能看到的Activity都是位于栈顶的Activity。
关于对话框:
android的对话框并不像其他吗,面向对象的MessageBox那么方便,在android中,对话框,也是一个Activity,故该对话框同样需要独立的XML(不是必须)、同样需要在
AndroidManifest.xml配置文件中添加一个Activity
然后再Activity结点中添加一个属性:
android:theme="@android:style/Theme.Dialog"
让该Activity的显示方式为对话框风格就行了。