學習Android不學習生命周期估計以後也不好混,原來學習ASP.,NET的時候發現有生命周期挺蛋疼的,不過還好Android Activity的生命周期比ASP.NET應用程序生命周期相對來說好理解一點,參考了Google的API,還有中文的博客的,加上自己一點見解,初學Android,高手可以忽略我~
Activity繼承了ApplicationContext,轉到定義,可以重寫一下方法:
* 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();
* }
新建了一個Lifecycle項目,MainActivity中重寫以上方法:
public class MainActivity extends Activity {
private static final String TAG = "com.example.lifecycle";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "生命周期Create");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i(TAG, "生命周期Start");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i(TAG, "生命周期Resume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i(TAG,"生命周期Pause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i(TAG,"生命周期Stop");
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i(TAG,"生命周期Destroy");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.i(TAG,"生命周期Restart");
}
public void test(View view){
Intent intent=new Intent(this,UserActivity.class);
startActivity(intent);
}
}