关于android activity 生命周期方法

http://developer.android.com

与其它编程范例里的 main() 方法不一样, android 在 activity 实例里的启动是通过调用一系列的回调方法,每一个回调方法都对应于生命周期中特定的状态。开启或者销毁一个activity 对通过一系列的 回调方法来实现。

1. onStart()

    onStart() 方法与 onStop() 方法是相对应的,因为不管是第一次创建还是从stopped 状态恢复回来都要调用 onStart(); 

    在onStart 方法里可以检测一些系统特征是否有效果,比如

    

    @Override

protectedvoid onStart(){
    super.onStart();  // Always call the superclass method first
    
    // The activity is either being restarted or started for the first time
    // so this is where we should make sure that GPS is enabled
    LocationManager locationManager = 
            (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
    if(!gpsEnabled){
        // Create a dialog here that requests the user to enable GPS, and use an intent
        // with the android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS action
        // to take the user to the Settings screen to enable GPS when they click "OK"
    }}@Overrideprotectedvoid onRestart(){
    super.onRestart();  // Always call the superclass method first
    
    // Activity being restarted from stopped state    }

2. onPause()

    Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns.    

      比如记录用户的视频播放点,草稿邮件等一些重要的东西都应该在 onPause() 里保存,因为 onPause(), onStop(), onDestory() 这三个方法中的任意一个被执行完后 activity 所在的 process 都有可能被系统杀死(比如为了恢复系统内存)。

3. onStop()

     基本上我们应该在 onStop() 方法里清除释放所有可能会导致 内存泄漏的 activity 资源;因为 onDestory() 可能不被执行,而又不太适合在 onPause() 里做太多的事情。

     Although the onPause() method is called before onStop(), you should use onStop() to perform larger,

     more CPU intensive shut-down operations, such as writing information to a database.

4. onDestory()

     onDestory() 是 activity 生命周期里最后一个 回调方法,所以onDestory() 是我们释放那些有可能导致内存泄漏的资源;

     通常在 onCreate() 里创建的后台 / 额外的线程对象,以及其它的 long-running actions like method tracing 都应该在 onDestory() 里得到释放。

   

 5. onSaveInstanceState()

     onSaveInstanceState() 在 onPause() 之前被调用;

     onRestoreInstanceState() 在 onStart() 之后被调用,且只有 Bundle savedInstanceState 不为空时才被系统调用;

     Activity的 onSaveInstanceState() 和 onRestoreInstanceState()并不是生命周期方法,它们不同于 onCreate()、onPause()等生命周期方法,它们并不一定会被触发。

      可参考 http://www.cnblogs.com/hanyonglu/archive/2012/03/28/2420515.html 

6.  

       如果在 onCreate() 里调用 finish() 方法会直接调用 onDestory() 方法;

      1)  启动Activity:onCreate()->onStart()->onResume()->Activity is running 

      2)  按back键返回:onPause()->onStop()->onDestroy() 再次启动时:onCreate()->onStart()->onResume()->Activity is running 
     

      3). 按home键返回:onPause()->onStop() 再次启动时:onRestart()->onStart()->onResume()->Activity is running 

      4) 切换到别的Activity(当前Activity不finish): onPause()->onStop() 再次启动时:onRestart()->onStart()->onResume()->Activity is running 

       5) 切换到别的Activity(当前Activity finish): onPause()->onStop()->onDestroy() 再次启动时:onCreate()->onStart()->onResume()->Activity is running 

    

猜你喜欢

转载自sunzeping.iteye.com/blog/1867653