当前位置:网站首页>Activity生命周期
Activity生命周期
2022-07-07 09:04:00 【砖厂打工仔】
Activity生命周期
一、生命周期图
二、回调方法
- onCteate():当Activity首次被创建时,会执行此方法。一般情况下会做一些初始化,输入事件的绑定等。
- onStart():当Activity显示给用户时,会执行此方法。此时用户可以看到该Activity,但不能进行交互,可以简单理解为看得见,摸不着。
- onResume():当Activity位于前台时,会执行此方法。此时用户可以和该Activity进行交互,并且这时的Activity位于栈顶。即看得见摸得着。
- onPause():当有另一个Activity覆盖当前Activity时,会执行此方法。之前的Activity会失去焦点并保存数据,并且是可见的,只是不能与用户交互了。
- onStop():当Activity不可见了,会执行onStop(),这时的Activity会在后台运行,并不会被销毁。
- onDestroy():当点击系统界面的返回键时,会执行Activity的finish()方法,这时Activity会被销毁。
- onRestart():当第二个Activity被销毁后,第一个Activity会回到前台,此时会执行此方法。但场景二对应的FirstActivity不会执行此方法,因为以弹窗形式显示的SecondActivity,并没有完全覆盖FirstActivity,所以FirstActivity没有执行onStop(),也就不会执行onRestart()方法了,原理可参照上面生命周期图。
三、实例演示
这里使用了两个Activity举例:FirstActivity、SecondActivity。重写了所有的生命周期方法,打印日志。
场景一:SecondActivity采用的与FirstActivity一样默认的全屏显示。
- 当启动FirstActivity时,会执行FirstActivity的onCteate()、onStart()、onResume()方法。如下:
- 点击FirstActivity中的第一个按钮,跳转至SecondActivity,此时FirstActivity会执行onPause()、onStop()方法,从而不可见。而SecondActivity会被创建,执行onCteate()、onStart()、onResume()方法。如下:
- 点击手机返回键,相当于执行的Activity的finish()方法。此时SecondActivity将被销毁,会执行onPause()、onStop()、onDestroy()方法。而FirstActivity会回到前台,会执行onRestart()、onStart()、onResume()方法。如下:
- 再次点击返回键,FirstActivity也会被销毁,相继执行onPause()、onStop()、onDestroy()方法。
场景二:SecondActivity采用弹窗的形式显示。
- 当启动FirstActivity时,会执行FirstActivity的onCteate()、onStart()、onResume()方法。如下:
- 点击FirstActivity中的第一个按钮,跳转至SecondActivity,此时FirstActivity只会执行onPause()方法,是部分可见的。而SecondActivity会被创建,执行onCteate()、onStart()、onResume()方法。如下:
- 点击手机返回键,相当于执行的Activity的finish()方法。此时SecondActivity将被销毁,会执行onPause()、onStop()、onDestroy()方法。而FirstActivity会由部分可见转为全部可见,只会执行onResume()方法。如下:
- 再次点击返回键,FirstActivity也会被销毁,相继执行onPause()、onStop()、onDestroy()方法。如下:
边栏推荐
- Get pictures through opencv, change channels and save them
- Still cannot find RPC dispatcher table failed to connect in virtual KD
- [untitled]
- Verilog 实现数码管显视驱动【附源码】
- Go-Redis 中间件
- 【C#】WinForm运行缩放(变糊)的解决方法
- Force buckle 1002 Find common characters
- [C #] the solution of WinForm operation zoom (blur)
- 从色情直播到直播电商
- Wallhaven wallpaper desktop version
猜你喜欢
Find the root of equation ax^2+bx+c=0 (C language)
Unity script visualization about layout code
Interprocess communication (IPC)
Still cannot find RPC dispatcher table failed to connect in virtual KD
uniCloud
2021-04-23
shardingsphere分库分表示例(逻辑表,真实表,绑定表,广播表,单表)
BUUCTF---Reverse---reverse1
Simple and easy to modify spring frame components
[untitled]
随机推荐
Transaction rolled back because it has been marked as rollback-only解决
uniapp 在onLaunch中跳转页面后,点击事件失效解决方法
What are the test preparation materials and methods for soft exam information processing technicians?
书签整理-程序员常用网站导航
How to successfully pass the senior system architecture designer in the second half of the year?
Poj1821 fence problem solving Report
The sixth training assignment
【推薦系統 01】Rechub
Get pictures through opencv, change channels and save them
2022.7.5DAY597
Force buckle 1002 Find common characters
长列表性能优化方案 memo
Unity websocket server
Bookmarking - common website navigation for programmers
MPX plug-in
The gun startles the dragon, and the crowd "locks" Zhou Zhi
JSON format query of MySQL
When initializing 'float', what is the difference between converting to 'float' and adding 'f' as a suffix?
Kitex retry mechanism
基于STC8G1K08的0.96寸IIC液晶屏驱动程序