72

Actually, I would say that both iOS ViewControllers and Android Activities have their lifecycle methods. For example an equivalent of ViewController.viewDidLoad() is Activity.onCreate() ?

Else I still need to know the equivalent of the other :

  • OnStart()
  • OnRestart()
  • OnResume()
  • OnStop()
  • OnDestroy()
  • OnPause()
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
TooCool
  • 10,598
  • 15
  • 60
  • 85
  • possible duplicate of [Looking to understand the iOS UIViewController lifecycle](http://stackoverflow.com/questions/5562938/looking-to-understand-the-ios-uiviewcontroller-lifecycle) – Greg Mar 10 '15 at 16:28

2 Answers2

193

This is a comparison between the lifecycle of Android vs iOS:

enter image description here

  • Note: viewDidUnload is deprecated after iOS 6
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 3
    Note: `viewDidUnload` is deprecated after iOS 6 – Ryan R Dec 06 '15 at 03:26
  • 21
    As far as I know, `viewWillAppear` and `viewDidAppear` are called when those views appear in the application window/context, not in the screen, it differs from onResume/onStart that are called every time the activity/fragment appears to the user. – Felipe Jun Mar 22 '16 at 15:07
  • A closer equivalent for `onCreate` might be both `loadView` + `viewDidLoad`. – Dean Kelly Apr 03 '16 at 16:46
  • More accurately onCreate is just loadView because you manually load the UI file and present it. After that point you are in `viewDidLoad` territory. – Allison Feb 18 '17 at 07:13
  • 6
    onStart() to ViewWillApear , onResume to ViewDidAppear, onPause to ViewWillDisapear, onStop to ViewDidDisapear, onRestart actually doesn't have equivalet in iOS. – Damyan Todorov Jun 02 '17 at 13:04
  • Be careful with onResume == ViewDidAppear, as onResume will be triggered every time Activity is resumed, but ViewController just the first time. – Pelanes Sep 26 '19 at 09:51
  • What about `awakeFromNib`? – Alwaysblue Jun 08 '21 at 09:26
0

Here's how to get it done the Droid way:

import Foundation
import SwiftUI
import UIKit

public class AppleActivity : UIViewController {
    
  
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        initialize()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initialize()
    }
    
    override public func viewDidLoad() -> Void {
        super.viewDidLoad()
        onCreate()
    }
    
    override public func viewWillAppear(_ animated: Bool) -> Void {
        super.viewWillAppear(animated)
        onStart()
    }

    
    public func initialize(){
        NotificationCenter.default.addObserver(self, selector: #selector(onStart), name: UIApplication.willEnterForegroundNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(onResume), name: UIApplication.didBecomeActiveNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(onPause), name: UIApplication.willResignActiveNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(onStop), name: UIApplication.didEnterBackgroundNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(onDestroy), name: UIApplication.willTerminateNotification, object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    
    public func onCreate() {
        print("onCreate()")
    }
    
    @objc public func onStart() {
        print("onStart()")
    }
    
    @objc public func onResume() {
        print("onResume()")       
    }
    
    @objc public func onPause() {
        print("onPause()")        
    }
    
    @objc public func onStop() {
        print("onStop()")         
    }
    
    @objc public func onDestroy() {
        print("onDestroy()")       
    }
    
    
}


struct AppleActivityStruct : UIViewControllerRepresentable {
    
    typealias UIViewControllerType = AppleActivity
    
    public func makeUIViewController(context : Context) -> AppleActivity {
        return AppleActivity()
    }
    
    public func updateUIViewController(_ uiViewController : AppleActivity, context : Context) {
            // Update the view controller here if needed
    }
}

Now you get all your callbacks triggered everytime you want it, no stress + no wahala

N.B
Since onStart() via NotificationCenter.default.addObserver(self, selector: #selector(onStart), name: UIApplication.willEnterForegroundNotification, object: nil) might not get invoked the first time the scene of the UIViewController is created, that's why a call to it is present within viewWillAppear(_ animated: Bool)

HangarRash
  • 7,314
  • 5
  • 5
  • 32
linker
  • 821
  • 1
  • 8
  • 20
  • There are many things about this that are wrong. As of iOS 13 and support for scenes, several of these UIApplication lifecycle events will not be called. And none of them are related to a view controller's lifecycle. – HangarRash Jul 26 '23 at 23:23
  • @HangarRash so far this works for iOS 16 on my device and simulator, it would be nice if you could point out the specifics about what’s not working with your implementation of this – linker Aug 13 '23 at 20:17