-
前言:
Activity的生命周期不是自身控制的,而是由Android系统控制的。系统通过调用不同的方法,完成Activity不同状态的转换。本文将通过一个实例来帮助理解Activity生命周期。
本文要点:
1.理解生命周期
2.Activity状态扭转
-
生命周期概述
先上图:
这幅图已经很完整的描述了Activity的生命周期,以及状态转换。但这里针对上图做几点说明:
(1)Activity生命周期中,有很多种状态。其中包括Resumed(运行态,可见且可交互),Paused(暂停,部分可见但不可交互),Stoped(停止态,不可见),Destroyed/Killed(销毁)等;
(2)Entire Lifetime,完整的生命周期从调用onCreate()开始,直至调用onDestroy()结束;
(3)Visible Lifetime,生命周期中可见的阶段,发生在调用onStart()之后,onStop()之前。处于Resumed,或Paused态,在这期间Activity处于屏幕上;
(4)Foreground Lifetime,生命周期可交互的阶段,在调用onResume()之后,onPause()之前。在这期间,Activity显示在所有其他Activity之前,并获取输入焦点。
(5)Android系统通过调用onCreate()方法创建Activity实例.onDestroy()方法释放,关闭Activity;
(6)Android系统通过栈管理Activities,屏幕最前端的Activity位于栈顶。不在栈顶的Activity一般处于Paused状态,Stoped状态,或者Destroyed中的一种;
(7)实际应用中,我们通过重载Android生命周期中不同的方法,实现对不同状态下Activity的控制。如:我们可以重载onCreate()方法,实现对数据库的初始化;重写onDestroy(),执行删除数据库操作等。
-
状态转换实例
为了便于理解,我简单的写了一个Demo,不明白Activity周期的朋友们,可以亲手实践一下,大家按照我的步骤来。
第一步:新建一个Android工程,我这里命名为MainActivity.再创建一个OtherActivity继承activity。
- public class MainActivityextends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- System.out.println("MainActivity:----------------onCreate--");
- Button button=(Button) this.findViewById(R.id.button);
- button.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- Intent intent=new Intent(MainActivity.this,OtherActivity.class);
- startActivity(intent);
- }
- });
- }
- @Override
- protected void onStart() {
- super.onStart();
- // The activity is about to become visible.
- System.out.println("MainActivity:----------------onStart--");
- }
- @Override
- protected void onResume() {
- super.onResume();
- // The activity has become visible (it is now "resumed").
- System.out.println("MainActivity:----------------onResume--");
- }
- @Override
- protected void onPause() {
- super.onPause();
- // Another activity is taking focus (this activity is about to be "paused").
- System.out.println("MainActivity:----------------onPause--");
- }
- @Override
- protected void onStop() {
- super.onStop();
- // The activity is no longer visible (it is now "stopped")
- System.out.println("MainActivity:----------------onStop--");
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- // The activity is about to be destroyed.
- System.out.println("MainActivity:----------------onDestroy--");
- }
- }
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println("MainActivity:----------------onCreate--");
Button button=(Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
}
});
}
@Override
protected void onStart() {
super.onStart();
// The activity is about to become visible.
System.out.println("MainActivity:----------------onStart--");
}
@Override
protected void onResume() {
super.onResume();
// The activity has become visible (it is now "resumed").
System.out.println("MainActivity:----------------onResume--");
}
@Override
protected void onPause() {
super.onPause();
// Another activity is taking focus (this activity is about to be "paused").
System.out.println("MainActivity:----------------onPause--");
}
@Override
protected void onStop() {
super.onStop();
// The activity is no longer visible (it is now "stopped")
System.out.println("MainActivity:----------------onStop--");
}
@Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
System.out.println("MainActivity:----------------onDestroy--");
}
}
- public class OtherActivityextends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.other);
- System.out.println("OtherActivity:----------------onCreate--");
- }
- @Override
- protected void onStart() {
- super.onStart();
- // The activity is about to become visible.
- System.out.println("OtherActivity:----------------onStart--");
- }
- @Override
- protected void onResume() {
- super.onResume();
- // The activity has become visible (it is now "resumed").
- System.out.println("OtherActivity:----------------onResume--");
- }
- @Override
- protected void onPause() {
- super.onPause();
- // Another activity is taking focus (this activity is about to be "paused").
- System.out.println("OtherActivity:----------------onPause--");
- }
- @Override
- protected void onStop() {
- super.onStop();
- // The activity is no longer visible (it is now "stopped")
- System.out.println("OtherActivity:----------------onStop--");
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- // The activity is about to be destroyed.
- System.out.println("OtherActivity:----------------onDestroy--");
- }
- }
public class OtherActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
System.out.println("OtherActivity:----------------onCreate--");
}
@Override
protected void onStart() {
super.onStart();
// The activity is about to become visible.
System.out.println("OtherActivity:----------------onStart--");
}
@Override
protected void onResume() {
super.onResume();
// The activity has become visible (it is now "resumed").
System.out.println("OtherActivity:----------------onResume--");
}
@Override
protected void onPause() {
super.onPause();
// Another activity is taking focus (this activity is about to be "paused").
System.out.println("OtherActivity:----------------onPause--");
}
@Override
protected void onStop() {
super.onStop();
// The activity is no longer visible (it is now "stopped")
System.out.println("OtherActivity:----------------onStop--");
}
@Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
System.out.println("OtherActivity:----------------onDestroy--");
}
}
第三步:运行上述工程,效果图如下(没什么特别的):

核心在Logcat视窗里,我们打开应用时先后执行了onCreate()->onStart()->onResume三个方法,看一下LogCat视窗如下:

点击go按钮:

一定跳转到了 另一个activity界面,下面让我们看看logcat:

当点击go按钮后首先执行mainActivity的onPause 然后依次执行otherActivity的 onCreate() onStart() onResume()方法,当整个屏幕被另一个activity完全遮挡住了 调用mainActivity的onStop方法.
接下来点击back按钮:

这一次先是调用了otherActivity的onPuse方法,失去焦点,然后调用mainActivity的onStart onResume 接着就是otherActivity的停止,销毁。
从上面可以看出onCreate方法只调用一次,当一个activity失去焦点时,也就是不在最前端时调用onPause方法, 当整个activity不可见时,也就是完全被另一个activity覆盖时,会调用onStop方法。
下面再让我们看下上面的Activity生命周期图是不是就容易理解了,当失去焦点时调用onPause方法,重新获得焦点调用OnResume方法 这两个方法是相对的。完全被覆盖调用onStop方法,返回前端调用onStart方法。
然后在点击home键验证一下:

OK!
本文通过一个简单实例详细解析了Android中Activity的生命周期,包括其不同状态的转换过程,以及如何通过重写特定方法来控制这些状态。

2025

被折叠的 条评论
为什么被折叠?



