For each fragment type, SAPUI5 provides a method that can be used to programmatically instantiate a fragment.
To give an example of a programmatic instantiation of a XML fragment, you first have to define one. The following code presents an example definition:
<Button xmlns= "sap.ui.commons" id= "btnInFragment" text= "Hello World" />
This fragment can be instantiated from a controller as follows:
var myButton = sap.ui.xmlfragment( "my.useful.VerySimpleUiPart" );
This specific fragment does not use a controller; if controls inside a fragment needs methods that are to be defined in a controller, the controller has to be referred to in an additional parameter.
Fragments can be instantiated from JSViews, as well. Fragments of any type can be used within views of any type.
var myButton = sap.ui.xmlfragment(this.getView().getId(), "my.useful.VerySimpleUiPart" ); var theSameButton = this.byId("btnInFragment"); // returns the button in the fragment