发布时间:2016-12-8 17:53:39 编辑 分享查询网我要评论

      Activity在Android是一个极其常用类,与用户交互离不开它。      我们先看一段Google对Activity的注释,开头有一段是这样的: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 [email protected] #setContentView}.  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 [email protected] android.R.attr#windowIsFloating} set) or embedded inside of another activity ([email protected] ActivityGroup}).      上面一段主要就是说,Activity主要是用来创建一个与用户交互的窗口,通过setContentView方法,可以将各种UI放在这个窗口上。Activity可以是全屏的窗口或者说是介面,也是可以悬浮的,也可以一个Activity嵌套包含在另一个Activity里形成一个Activity组合(ActivityGroup)。这里要说一下是,ActivityGroup,Google已建议在HONEYCOMB以后的开发中,用Fragment和FragmentManager来代替。 接下来,看下面一段注解:There are two methods almost all subclasses of Activity will implement:[email protected] #onCreate} is where you initialize your activity.  Most importantly, here you will usually [email protected] #setContentView(int)} with a layout resource defining your UI, and [email protected] #findViewById} to retrieve the widgets in that UI that you need to interact with [email protected] #onPause} is where you deal with the user leaving your activity.  Most importantly, any changes made by the user should at this  point be committed (usually to [email protected] android.content.ContentProvider} holding the data). 上面说Activity的子类一般都会覆写onCreate和onPause两个方法。其中onCreate主要用来初始化Activity所需要的各个UI组件,onPause则可以在用户离开介面的时候保存数据。      下面我们主要看一下Activity的生命周期:      1,Activity的整个生命周期。从onCreate开始初始化,到onDestroy结束释放所有数据。 附上Google注解:The <b>entire lifetime</b> of an activity happens between the first call [email protected]} through to a single final call [email protected]}.  An activity will do all setup of "global" state in onCreate(), and release all remaining resources in onDestroy().  For example, if it has a thread running in the background to download data from the network, it may create that thread in onCreate() and then stop the thread in onDestroy().      2,可视化的生命周期。这个生命周期发在onStart调用开始到onStop调用结束之间。而且在Activity的显示和隐藏的切换过程中,这两个方法可以被多次调用。附上Google注解:The <b>visible lifetime</b> of an activity happens between a call [email protected]} until a corresponding call [email protected]}.  During this time the user can see the activity on-screen, though it may not be in the foreground and interacting with the user.  Between these two methods you can maintain resources that are needed to show the activity to the user.  For example, you can register [email protected] android.content.BroadcastReceiver} in onStart() to monitor for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are displaying.  The onStart() and onStop() methods can be called multiple times, as the activity becomes visible and hidden to the user.      3,前台生命周期。这个生命周期出现在onResume和onPause方法调用之间。onResume时,Activity处于最前端,也是处于ActivityStack的顶部,用户可以写之交互。由于这两个方法的切换比较频繁,所经代码要尽量是轻量的,能够瞬时完成的,也就是说非耗时的。附上Google注解:The <b>foreground lifetime</b> of an activity happens between a call [email protected]} until a corresponding call [email protected]}.  During this time the activity is in front of all other activities and interacting with the user.  An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight.      究竟Activity在什么时候保存各种状态和数据,这里也是有说明的,看下面一段:The entire lifecycle of an activity is defined by the following Activity methods.  All of these are hooks that you can override to do appropriate work when the activity changes state.  All activities will [email protected]} to do their initial setup; many will also [email protected]} to commit changes to data and otherwise prepare to stop interacting with the user.  You should always call up to your superclass when implementing these methods.      由此可以看出,Activity都是在onCreate中初始化数据,在onPause中保存数据,暂停与用户的交互。上面说的following Activity methods 又是哪些方法呢,下面列出来: 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(); }       以下一段代码是注解中的例子,说的是在onPause方法中保存信息: public class CalendarActivity extends Activity { ... static final int DAY_VIEW_MODE = 0; static final int WEEK_VIEW_MODE = 1; private SharedPreferences mPrefs; private int mCurViewMode; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences mPrefs = getSharedPreferences(); mCurViewMode = mPrefs.getInt("view_mode", DAY_VIEW_MODE); } protected void onPause() { super.onPause(); SharedPreferences.Editor ed = mPrefs.edit(); ed.putInt("view_mode", mCurViewMode); ed.commit(); } }      关于Activity的其他种种,都可以去参照Google的Activity源码以获得更多的信息。