iOS和Android对比系列(一):生命周期

iOS UIViewControllerAndroid Activity 生命周期对比

iOS UIViewController Android Activity 描述
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 初始化
- (instancetype)init 初始化
- (void)loadView; 加载view
- (void)viewDidLoad; onCreate() view加载完成
- (void)viewWillAppear:(BOOL)animated; onStart() view将要显示
- (void)viewDidAppear:(BOOL)animated; onResume() view已经显示了
- (void)viewWillDisappear:(BOOL)animated; onPause() view将要消失
- (void)viewDidDisappear:(BOOL)animated; onStop() view已经消失
- (void)dealloc onDestroy() 销毁

iOSAndroid大体上的逻辑是相同的,细微上会有一些差别;

iOS UIViewController

  • - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  • - (instancetype)init
  • - (void)loadView;
  • - (void)viewDidLoad;
  • - (void)viewWillAppear:(BOOL)animated;
  • - (void)viewDidAppear:(BOOL)animated;
  • - (void)viewWillDisappear:(BOOL)animated;
  • - (void)viewDidDisappear:(BOOL)animated;
  • - (void)dealloc

打印如下:

-[MyViewController initWithNibName:bundle:]
-[MyViewController init]
-[MyViewController loadView]
-[MyViewController viewDidLoad]
-[MyViewController viewWillAppear:]
-[MyViewController viewDidAppear:]
-[MyViewController viewWillDisappear:]
-[MyViewController viewDidDisappear:]
-[MyViewController dealloc]

present方式

下面演示了从源控制器 OneViewController present 到目标控制器 MyViewController ,再dismiss返回 OneViewController的过程

OneViewController 加载完成显示顺序:

-[OneViewController initWithNibName:bundle:]
-[OneViewController init]
-[OneViewController loadView]
-[OneViewController viewDidLoad]
-[OneViewController viewWillAppear:]
-[OneViewController viewDidAppear:]

OneViewController 通过 present 方式到 MyViewController 过程如下:

-[MyViewController initWithNibName:bundle:]
-[MyViewController init]
-[MyViewController loadView]
-[MyViewController viewDidLoad]
-[OneViewController viewWillDisappear:]
-[MyViewController viewWillAppear:]
-[MyViewController viewDidAppear:]
-[OneViewController viewDidDisappear:]

可以看到需要等到目标控制器完成 viewDidLoad 方法(即加载 view 完成),源控制器才会 Disappear,目标控制器 Appear

目标控制器 dismiss 过程如下,即从目标控制器返回到源控制器:

-[MyViewController viewWillDisappear:]
-[OneViewController viewWillAppear:]
-[OneViewController viewDidAppear:]
-[MyViewController viewDidDisappear:]
-[MyViewController dealloc]

可以看到,目标控制器 WillDisappear ,等到源控制器 viewDidAppear 了,目标控制器才会真正的 DidDisappear 和销毁 dealloc

小结

  • 同一个时刻,只有一个控制器 viewDidAppear ,只有一个控制器 viewDidAppear 另外一个控制器才会 viewDidDisappear
  • 一个控制器 AviewWillDisappear 作为一个信号,另外一个控制器 B 才会 viewWillAppearviewDidAppear,控制器B viewDidAppear 作为信号,控制器 A 才会 viewDidDisappear (可能还有 dealloc)
-[A viewWillDisappear:]
-[B viewWillAppear:]
-[B viewDidAppear:]
-[A viewDidDisappear:]
-[A dealloc]

navigation方式

下面演示了从源控制器 OneViewController 到目标控制器 MyViewController ,再返回 ViewController的过程

OneViewController控制器加载顺序: 和 present方式相同

-[OneViewController initWithNibName:bundle:]
-[OneViewController init]
-[OneViewController loadView]
-[OneViewController viewDidLoad]
-[OneViewController viewWillAppear:]
-[OneViewController viewDidAppear:]

OneViewController pushMyViewController

-[MyViewController initWithNibName:bundle:]
-[MyViewController init]
-[MyViewController loadView]
-[MyViewController viewDidLoad]
-[OneViewController viewWillDisappear:]
-[MyViewController viewWillAppear:]
-[OneViewController viewDidDisappear:]
-[MyViewController viewDidAppear:]

MyViewController popOneViewController

-[MyViewController viewWillDisappear:]
-[OneViewController viewWillAppear:]
-[MyViewController viewDidDisappear:]
-[OneViewController viewDidAppear:]
-[MyViewController dealloc]

对比:

UIViewController生命周期对比.png

总结:

  • 控制器A 切换到 到 控制器B 的大致顺序为:
    • 1、[A viewWillDisappear]
    • 2、[B viewWillAppear]
    • 3、[B viewDidAppear]
    • 4、[A viewDidDisappear]
  • 如果是 present/dismiss 方式,顺序为 1234
  • 如果是 push/pop 方式,顺序为1243

1、需要一个控制器 viewWillDisappear 作为信号,另外一个控制器开会开始处理;
2、控制器A到控制器B

  • present/dismiss方式,需要 B viewDidAppear,然后A viewDidDisappear
  • push/pop方式,需要 A viewDidDisappear,然后 B viewDidAppear

Android Activity

参考官方图示

[图片上传失败...(image-6cfcaf-1536829354686)]

activity 基本的四种状态

  • resumed : If an activity is in the foreground of the screen (at the top of the stack), it is active or running.
  • paused: If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
  • stopped: If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
  • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.

下面为一开始进入MainActivity,跳转到 LoginActivity,又返回到 MainActivity

// 1、MainActivity 创建
E/MainActivity: onCreate -- 
E/MainActivity: onStart -- 
E/MainActivity: onResume -- 

// 2、MainActivity 跳转到 LoginActivity
E/MainActivity: onPause -- 
E/LoginActivity: onCreate -- 
E/LoginActivity: onStart -- 
E/LoginActivity: onResume -- 
E/MainActivity: onStop -- 

// 3、LoginActivity 返回到 MainActivity
E/LoginActivity: onPause -- 
E/MainActivity: onRestart --
E/MainActivity: onStart -- 
E/MainActivity: onResume -- 
E/LoginActivity: onStop -- 
E/LoginActivity: onDestroy -- 
  • onCreate() : activity 第一次被创建的时候,一般用作创建views绑定数据、获取Bundle里面的数据等(页面传递的数据)
    • 下一步: onStart()
  • onRestart() : activitystopped,然后又需要重新 onStart(),看上面的例子
    • 下一步: onStart()
  • onStart() : activity 变的对用户可见visible
    • 下一步:
      • onResume() : activity 进入前台 foreground
      • onStop() : activity 被隐藏了 hidden,不可见了 (iOS没有这个逻辑?)
  • onResume() : activityactivity stack的最上面,可以和用户进行交互了;
    • 下一步 onPause()
  • onPause() : 将要跳转到另外一个activity,包含跳转和返回
    • 下一步:
      • onResume() : 返回
      • onStop() : activity 被隐藏了 hidden,不可见了 (iOS没有这个逻辑?)
  • onStop() : activity 对用户不可见了;1、开启了一个新的activity覆盖在当前的activity上面;2、从当前的activity返回到上一个activity
    • 下一步:
      • onRestart() : 类似 2 情况的 上一个activity
      • onDestroy() : 类似 2 情况的 当前的activity
  • onDestroy() : activity 被销毁了,执行 finish()

小结


页面A 跳转到 页面B

  • iOS : 页面B 会走到 view加载完成方法 (viewDidLoad),然后页面A才会将要消失(viewWillDisappear)
  • Android : 然后页面A才会将要消失(onPause),然后才会走页面B的方法

页面B 跳转到 页面A类似

Android类似iOSpresent方式跳转页面: 都是目标控制器已经显示了,源控制器才会已经消失了;


AndroidiOS多了一个onRestart()方法,即重新启动