一个iOS开发者的android修行之路一:UIViewController与Activity的生命周期对比探究

先说说自己学习安卓的一个初衷,在移动app开发的过程中,经常和android开发人员讨论一些技术的实现,有时候交流起来就特费劲,不是他听不懂我说的术语,就是他听不懂我讲的什么,反正就是有点语言不通,比如:iOS中的集合视图UITableView、UICollectionView中每个子项一般叫做cell,讨论的时候也叫做cell中干啥干啥,而安卓中的集合视图ListView,GridView,RecyclerView的每一个子项叫做item,这个还是小的差异;还有就是讨论某个UI界面数据刷新是什么时机进行刷新的,我说我们这边是在viewWillAppear的时候进行刷新,然后android那边就懵逼了,直到我换了一种说法,就是在这个页面即将显示的时候刷新数据,对方才听懂;而有时候我们听到对方的一些名词的时候,会想这Activity是什么东东?Fragment又是什么?于是我们会联想这对应到我们iOS这边相应的是什么。总之,诸如此类的听到对方说的一些名词自己云里雾里的。其实,一个更为重要的原因是想要成为移动开发的leader,你不懂一些android的开发,以及了解android和iOS的一些差异,你是没法胜任这个工作的。有时候,android遇到一些坑时,你必须知道如何高效的解决。如何和小组开发人员探讨某个问题的最佳实践。

Activity的生命周期

一个优秀app必须兼顾两个必要条件:1、优秀的界面,2、良好的用户交互体验。
在iOS中是由UIViewController主要负责加载界面呈现给用户,以及处理用户的交互逻辑;而安卓负责界面展示和交互功能就是Android系统四大组件之一的活动Activity
1. 我们先看下iOS的UIViewController的生命周期函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
class BaseViewController: UIViewController {

// 前两个init方法是UIViewController的初始化方法
// 非storyboard加载会调用此方法
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
print("\(self)\(#function)")
}

// 使用storyboard加载的UIViewController会调用此初始化方法
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print("\(self)\(#function)")
}

// 视图的@IBOutlet和@IBAction链接完成再次方法中
override func awakeFromNib() {
super.awakeFromNib()
print("\(self)\(#function)")
}

// Creates the view that the controller manages.
// 此方法一般调用的少
override func loadView() {
super.loadView()
print("\(self)\(#function)")
}

// Called after the controller'€™s view is loaded into memory.
// 控制器视图加载完成(加载到了内存)的时候调用,只被调用一次,我们一般在这个方法中进行数据的网路请求,约束的设置,以及视图的一些操作等等。
override func viewDidLoad() {
super.viewDidLoad()
print("\(self)\(#function)")
}

// MARK: -分割线----上面的方法只会调用一次,下面的方法除了deinit方法可能会调用多次

// Notifies the view controller that its view is about to be added to a view hierarchy.
// 视图即将显示的时候调用
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("\(self)\(#function)")
}

// Called to notify the view controller that its view is about to layout its subviews.
// 控制器视图即将布局他的子视图时调用
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
print("\(self)\(#function)")
}

// Called to notify the view controller that its view has just laid out its subviews.
// 控制器视图已经布局完其子视图后调用,在iphone X的适配就是在此方法中获取view.safeAreaInsets进行处理的,
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("\(self)\(#function)")
}

// Notifies the view controller that its view was added to a view hierarchy.
// 试图已经完全显示的时候调用
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("\(self)\(#function)")
}

// Notifies the view controller that its view is about to be removed from a view hierarchy.
// 视图即将被移除的时候调用
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("\(self)\(#function)")
}

// Notifies the view controller that its view was removed from a view hierarchy.
// 视图已经被完全移除的时候调用
override func viewDidDisappear(_ animated: Bool) {
super.viewDidAppear(animated)
print("\(self)\(#function)")
}

// Sent to the view controller when the app receives a memory warning.
// 手机收到内存警告的时候调用
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
print("\(self)\(#function)")
}

// 视图被销毁的时候调用,在这里可以执行的操作有:KVO移除,通知的移除,定时器的销毁
deinit {
print("\(self)\(#function)")
}

}

下面是从ViewController跳转到SecondViewControler在跳转到ThirdController控制台的log:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>init(coder:)
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>awakeFromNib()
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>loadView()
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>viewDidLoad()
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>viewWillAppear
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>viewWillLayoutSubviews()
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>viewDidLayoutSubviews()
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>viewWillLayoutSubviews()
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>viewDidLayoutSubviews()
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>viewDidAppear
<LifecycleDemo.SecondViewController: 0x7fd122c16480>init(coder:)
<LifecycleDemo.SecondViewController: 0x7fd122c16480>awakeFromNib()
<LifecycleDemo.SecondViewController: 0x7fd122c16480>loadView()
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewDidLoad()
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>viewWillDisappear
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewWillAppear
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewWillLayoutSubviews()
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewDidLayoutSubviews()
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewWillLayoutSubviews()
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewDidLayoutSubviews()
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>viewDidDisappear
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewDidAppear
<LifecycleDemo.ThirdViewController: 0x7fd122c2ff00>init(nibName:bundle:)
<LifecycleDemo.ThirdViewController: 0x7fd122c2ff00>loadView()
<LifecycleDemo.ThirdViewController: 0x7fd122c2ff00>viewDidLoad()
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewWillDisappear
<LifecycleDemo.ThirdViewController: 0x7fd122c2ff00>viewWillAppear
<LifecycleDemo.ThirdViewController: 0x7fd122c2ff00>viewWillLayoutSubviews()
<LifecycleDemo.ThirdViewController: 0x7fd122c2ff00>viewDidLayoutSubviews()
<LifecycleDemo.ThirdViewController: 0x7fd122c2ff00>viewWillLayoutSubviews()
<LifecycleDemo.ThirdViewController: 0x7fd122c2ff00>viewDidLayoutSubviews()
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewDidDisappear
<LifecycleDemo.ThirdViewController: 0x7fd122c2ff00>viewDidAppear
<LifecycleDemo.ThirdViewController: 0x7fd122c2ff00>viewWillDisappear
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewWillAppear
<LifecycleDemo.ThirdViewController: 0x7fd122c2ff00>viewDidDisappear
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewDidAppear
<LifecycleDemo.ThirdViewController: 0x7fd122c2ff00>deinit
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewWillDisappear
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>viewWillAppear
<LifecycleDemo.SecondViewController: 0x7fd122c16480>viewDidDisappear
<LifecycleDemo.ViewController: 0x7fd122d0b5d0>viewDidAppear
<LifecycleDemo.SecondViewController: 0x7fd122c16480>deinit

2. 我们再来看下android的Activity的生命周期函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
public class BaseActivity extends AppCompatActivity {

String str = this.getClass().getName();

/*活动第一次被创建的时候调用,布局的加载,事件的绑定可以在这里完成,此时活动还是不可见的*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(str,"onCreate");
}

/*活动由不可见变为可见的时候调用*/
@Override
protected void onStart() {
super.onStart();
Log.i(str,"onStart");
}

/*活动已经完全可见,并且可以与用户交互,活动处于返回栈的栈顶*/
@Override
protected void onResume() {
super.onResume();
Log.i(str,"onResume");
}

/*在系统准备启动或者恢复一个activity时调用*/
@Override
protected void onPause() {
super.onPause();
Log.i(str,"onPause");
}

/*当activity 完全不可见的时候调用*/
@Override
protected void onStop() {
super.onStop();
Log.i(str,"onStop");
}

/*活动被重新启动的时候调用*/
@Override
protected void onRestart() {
super.onRestart();
Log.i(str,"onRestart");
}

/*acitivity被销毁前调用,调用之后,activity变为销毁状态*/
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(str,"onDestroy");
}

}

下面是一个MainActivity跳转到SecondActivity,然后在回来的生命函数调用在控制台输出的log:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
I/com.example.xcqromance.lifecycledemo.MainActivity: onCreate
I/com.example.xcqromance.lifecycledemo.MainActivity: onStart
I/com.example.xcqromance.lifecycledemo.MainActivity: onResume
I/com.example.xcqromance.lifecycledemo.MainActivity: onPause
I/com.example.xcqromance.lifecycledemo.SecondActivity: onCreate
I/com.example.xcqromance.lifecycledemo.SecondActivity: onStart
I/com.example.xcqromance.lifecycledemo.SecondActivity: onResume
I/com.example.xcqromance.lifecycledemo.MainActivity: onStop
I/com.example.xcqromance.lifecycledemo.SecondActivity: onPause
I/com.example.xcqromance.lifecycledemo.MainActivity: onRestart
I/com.example.xcqromance.lifecycledemo.MainActivity: onStart
I/com.example.xcqromance.lifecycledemo.MainActivity: onResume
I/com.example.xcqromance.lifecycledemo.SecondActivity: onStop
I/com.example.xcqromance.lifecycledemo.SecondActivity: onDestroy

通过上面的对比,我整理出iOS和android的生命周期函数有及其相似的地方和不同之处:iOS和android生命周期函数对比图

GithubDemo地址,里面有上述操作的代码,文章写的有点粗糙,同时欢迎大家一切探讨。一起添加iOS和android的生命周期的异同。