首先,了解Activity是什么?
Activity
是支持屏幕或UI的Java代码。换句话说,用户界面的构建块就是Activity
。 Activity
类是Android中的一个预定义类,每个拥有UI的应用程序都必须继承它来创建窗口。
例如,MainActivity
类是一个Activity
,它从库中的Activity
类扩展而来。 下图说明了一个Activity
的生命周期。
Activity
的一个生命周期描述了一个Activity
开始启动后的进程,直到应用程序关闭。 包括Activity
暂时停止的进程,恢复等等。
991141221_91702.png
示例代码:
package com.yiibai.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
public static final String TAG ="MyMessage";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Print Log
Log.i(TAG,"onCreate");
}
@Override
protected void onStop() {
super.onStop();
// Print Log
Log.i(TAG,"onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
// Print Log
Log.i(TAG,"onDestroy");
}
@Override
protected void onPause() {
super.onPause();
// Print Log
Log.i(TAG,"onPause");
}
@Override
protected void onResume() {
super.onResume();
// Print Log
Log.i(TAG,"onResume"); }
@Override
protected void onStart() {
super.onStart();
// Print Log
Log.i(TAG,"onStart");
}
@Override
protected void onRestart() {
super.onRestart();
// Print Log
Log.i(TAG,"onRestart"); }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// Print Log
Log.i(TAG, "onCreateOptionsMenu");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Print Log
Log.i(TAG, "onOptionsItemSelected");
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}