SAPUI5: What Is the Life Cycle of a Controller? (+Example)

This is about the life cycle of SAPUI5 controllers.

Understand the life cycle and its hooks.

Knowing the SAPUI5 controller life cycle is one of the SAPUI5 fundamentals.

Let’s get right into it!

The Life Cycle of an SAPUI5 controller

SAPUI5 controllers provide lifecycle hooks.

Those hooks are based on the life cycle of the controller’s view.

A view starts the process of instantiating the controller:

  • Application invokes a view
  • Instantiate the view and its controller
  • Load controller
  • onInit hook (after the view is instantiated)
  • onBeforeRendering hook (before the view is rendered)
  • onAfterRendering hook (after the view is rendered)
  • onExit hook (after the view is destroyed)
  • Controller is destroyed

The onInit and onExit methods are called exactly once per view instantiation.

The onBeforeRendering and onAfterRendering methods are called every time a view is invoked.

For example, invoke a method every when the view is instantiated in the controller’s onInit method:

// in your controller
 
...

// hook gets invoked when the view is instantiated
onAfterRendering: function() {
 
  this.fooBar();
 
},
 
fooBar: function() {
 
  sap.base.Log.info("foo bar");
    
}
  
...

Or, you can invoke a method every time after the view is rendered in the controller’s onAfterRendering method:

// in your controller
 
...

// hook gets invoked after the view is rendered  
onAfterRendering: function() {
 
  this.fooBar();
 
},
 
fooBar: function() {
 
  sap.base.Log.info("foo bar");
    
}
  
...

Here is the order in which an application executes in SAPUI5.

And here is the life cycle of a control in SAPUI5.

Leave a Comment