Android四大组件之Activity详解
什么是Activity
Activity是一种Android四大组件之一,每个Activity代表app的一个UI界面,用户可以与之进行交互。它是实现用户界面的核心组件之一,相当于Windows中的一个窗口或Frame。
如何创建Activity
通过Android Studio创建Activity
Android Studio是专门为Android开发打造的IDE,它内置的创建Activity功能使得我们在创建Activity上变得非常容易。以创建一个名为MainActivity的Activity为例,操作步骤如下:
- 打开Android Studio,选择File->New->New Project,输入工程名和包名等信息。
- 在project结构中,右键app文件夹,选择New->Activity->Empty Activity,输入Activity名MainActivity,点击Finish。
通过上述步骤,我们成功创建了一个名为MainActivity的Activity。
通过继承Activity创建Activity
我们同样可以通过继承Activity来创建自己的Activity。以一个名为MyActivity的Activity为例,代码如下:
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
}
生命周期
Activity有生命周期,即Activity从创建到销毁的过程就是它的生命周期。Activity的生命周期是从系统广播中心得到的信号,类似生命的重要器官的心跳一样。生命周期方法如下:
- onCreate:Activity被创建时调用
- onStart:Activity变成可见状态时调用
- onResume:Activity获得焦点时调用
- onPause:Activity失去焦点时调用
- onStop:Activity不再可见时调用
- onDestroy:Activity被销毁时调用
生命周期示例1
以一个由MainActivity启动MyActivity的例子来说明Activity的生命周期。
MainActivity.java代码:
public class MainActivity extends Activity {
private Button mButton;
private Intent mIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.button);
mIntent = new Intent(this,MyActivity.class);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(mIntent);
}
});
}
}
MyActivity.java代码:
public class MyActivity extends Activity {
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Log.d(TAG,"onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
}
运行后,点击MainActivity中的Button,可以看到MyActivity的生命周期。
生命周期示例2
以一个跨越多个Activity的例子来说明Activity的生命周期。
AppActivity.java代码:
public class AppActivity extends Activity {
private Button mButton;
private Intent mIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app);
mButton = findViewById(R.id.button);
mIntent = new Intent(this,MainActivity.class);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(mIntent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.d("AppActivity","onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d("AppActivity","onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d("AppActivity","onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d("AppActivity","onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("AppActivity","onDestroy");
}
}
MainActivity.java代码:
public class MainActivity extends Activity {
private Button mButton;
private Intent mIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.button);
mIntent = new Intent(this,MyActivity.class);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,AppActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
Log.d("MainActivity","onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d("MainActivity","onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d("MainActivity","onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d("MainActivity","onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("MainActivity","onDestroy");
}
}
MyActivity.java代码:
public class MyActivity extends Activity {
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Log.d(TAG,"onCreate");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onStart");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG,"onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG,"onDestroy");
}
}
运行后,可以通过log查看各个Activity的生命周期。点击Button后,MainActivity和MyActivity才会经过生命周期,而AppActivity在之前已经被创建过,因此只展现了onStart和onResume周期。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android四大组件之Activity详解 - Python技术站