博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
快速掌握activity的生命周期
阅读量:7229 次
发布时间:2019-06-29

本文共 3485 字,大约阅读时间需要 11 分钟。

activity的生命周期不管是在面试还是在工作中我们都会经常遇到,这当然也是非常基础的,基础也很重要哦,学会activity的生命周期对我们以后开发更健壮的程序会有很大帮助。下面来看一下Activity生命周期图:

\

为了便于理解,我简单的写了一个Demo,不明白Activity周期的朋友们,可以亲手实践一下,大家按照我的步骤来。

第一步:新建一个Android工程,我这里命名为MainActivity.再创建一个OtherActivity继承activity。

源码打印?

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 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键验证一下:

\

转载地址:http://yzdfm.baihongyu.com/

你可能感兴趣的文章
python 模块 chardet下载及介绍(转)
查看>>
能力工场--关于在JavaScript中使用EL表达式的问题
查看>>
NFS服务器设置
查看>>
s:iterator 中的status 使用方法
查看>>
cocos2d-x 源码剖析系列
查看>>
IT系统架构设计
查看>>
Nginx虚拟主机配置实践(一)
查看>>
细谈Spring(一)spring简介
查看>>
网络工程师的面试题
查看>>
nginx启动脚本
查看>>
常用输入法框架简介
查看>>
记录新机房建设。20130629
查看>>
安装ntop
查看>>
ssh远程登录讲解
查看>>
mysql的备份脚本
查看>>
linux下mysql的root密码忘记解决方法
查看>>
7.索引的性能分析
查看>>
在 Delphi 下使用 DirectSound (17): 频率均衡效果器 IDirectSoundFXParamEq8
查看>>
文件操作命令一cp 2
查看>>
Multi-Mechanize工程目录结构说明
查看>>