原文:http://www.2cto.com/kf/201112/113331.html
在AndroidManifest.xml文件中的<intent-filter>元素中有這么兩句:
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
你知道中間的兩句話都是什么含義么?你知道安卓應用的執行流程嗎?
當寫好的應用發布到手機上之后,當雙擊”抽屜“里該應用的圖標時,系統會將這個點擊時間包裝成一個Intent,該Intent包含兩個參數,如上所述的兩個參數被傳遞給應用之后,在應用的功能清單文件中尋找與該意圖匹配的意圖過濾器,如果匹配成功,找到相匹配的意圖過濾器所在的Activity元素,再根據<activity>元素的”name“屬性來尋找其對應的Activity類。接着Android操作系統創建該Activity類的實例對象,對象創建完成之后,會執行到該類的onCreate方法,此onCreate方法是重寫父類Activity的onCreate方法而實現的。onCreate方法用來初始化Activity實例對象。如下是helloWorld.java類中的onCreate方法的代碼:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
其中super.onCreate(savedInstanceState)的作用是調用其父類Activity的onCreate方法來實現對界面的圖畫繪制工作。在實現自己定義的Activity子類的onCreate方法時一定要記得調用該方法,以確保能夠繪制界面。
setContentView(R.layout.main)的作用是加載一個界面。該方法中傳入的參數是”R.layout.main“,其含義為R.java類中靜態內部類layout的靜態常量main的值,而改值是一個指向res目錄下的layout子目錄下的main.xml文件的標識符。因此代表着顯示main.xml所定義的畫面。
第二篇 onCreate(Bundle savedInstanceState)參數Bundle
原文地址 http://blog.sina.com.cn/s/blog_797cd06b01012ein.html
onCreate的方法是在Activity創建時被系統調用,是一個Activity生命周期的開始。可是有一點容易被忽視,就是onCreate方法的參數saveInsanceState。因為在一般的程序開發中,很少用到這個參數。onCreate方法的完整定義如下:
super.onCreate(saveInsanceState);
}
從字面上看saveInsanceState ,是保存實例狀態的。實際上,saveInsanceState也就是保存Activity的狀態的。那么,saveInsanceState中的狀態數據是從何處而來的呢?下面我們介紹Activity的另一個方法saveInsanceState 。
onsaveInsanceState方法是用來保存Activity的狀態的。當一個Activity在生命周期結束前,會調用該方法保存狀態。這個方法有一個參數名稱與onCreate方法參數名稱相同。如下所示 :
super.onSaveInsanceState(saveInsanceState);
}
int count = cvec.size();
int[] rawArray = new int[count * 2];
for ( int index = 0; index < count; index++) {
Coordinate c = cvec.get(index);
rawArray[2 * index] = c.x;
rawArray[2 * index + 1] = c.y;
}
return rawArray;
}
public Bundle saveState() {
Bundle map = new Bundle();
map.putIntArray("mAppleList", coordArrayListToArray(mAppleList));
map.putInt("mDirection", Integer.valueOf(mDirection));
map.putInt("mNextDirection", Integer.valueOf(mNextDirection));
map.putLong("mMoveDelay", Long.valueOf(mMoveDelay));
map.putLong("mScore", Long.valueOf(mScore));
map.putIntArray("mSnakeTrail", coordArrayListToArray(mSnakeTrail));
return map;
}
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.snake_layout);
mSnakeView = (SnakeView) findViewById(R.id.snake);
mSnakeView.setTextView((TextView) findViewById(R.id.text));
if (savedInstanceState == null) {
// We were just launched -- set up a new game
mSnakeView.setMode(SnakeView.READY);
} else {
// We are being restored
Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
if (map != null) {
mSnakeView.restoreState(map);
} else {
mSnakeView.setMode(SnakeView.PAUSE);
}
}
}
publicvoid onSaveInstanceState(Bundle outState) {
//Store the game state
outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
}
更多參考:
http://blog.csdn.net/randyjiawenjie/article/details/6651437http://hi.baidu.com/yplovelt/item/a43c1edb0d9c0de154347f8c
http://www.cnblogs.com/salam/archive/2010/10/27/1862730.html