在Activity执行 onCreate时可以使用 SDK_INT.....

    // Make sure we're running on Honeycomb or higher to use ActionBar APIs
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // For the main activity, make sure the app icon in the action bar
        // does not behave as a button
        ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(false);
    }

注意:在Android2.0(API 5 Level)或者更高版本中,使用SDK_INT阻止旧的操作系统执行新的API函数,老版本的版本将会遇到“运行时异常”的错误。

Activity的第一个生命周期函数是onCreated,最后一个生命周期函数是onDestroy()。系统将调用onDestroy()函数作为Activity从内存中完全销毁去除的最后一个信号。大多数的应用程序并不需要使用到这个函数,因为局部类的引用将会和Activity一起销毁,而一些清理工作,主要也是在onPaused()和onStop()中。然而,如果你的Activity包含了有在onCreated()或者其他持续性运行的资源在后台线程行运行,您就应该在onDestroy()方法中清除掉它们,而避免内存泄露。

@Override
public void onDestroy() {
    super.onDestroy();  // Always call the superclass
 
    // Stop method tracing that the activity started during onCreate()
    android.os.Debug.stopMethodTracing();
}

注意:一般来说,系统会在调用onPause()方法和onStop()之后才调用onDestory()方法,但是,有一种情况是例外的,那就是在你在onCreate()方法中调用finish()方法。在一些情况下,你的Activity作为一个临时决定的操作对象而去启动另外一个Activity时,你可能需要在onCreate()方法中调用finish()方法去销毁Activity,在这种情况中,系统会直接调用onDestroy()方法而不用再去调用其他的生命周期函数。

猜你喜欢

转载自zyzzsky.iteye.com/blog/1759405
今日推荐