Skip to main content

PortalBuilder

The PortalBuilder class is used to create a Portal instance. It follows a Builder Pattern to construct a portal using values passed in at runtime. PortalBuilder can be used in situations where you want to programmatically create a single Portal at runtime rather than defining it via XML. For managing multiple Portal instances in an application, it is recommended to use the PortalManager class.

An example of how to create a Portal using the PortalBuilder class is directly below.

val portal: Portal = PortalBuilder("myPortal")
.addPlugin(MyCapacitorPlugin::class.java)
.setPortalFragmentType(MyFadeInOutPortalFragment::class.java)
.setInitialContext(mapOf("myVariableFromAndroid" to 42))
.setStartDir("web_app")
.create()

Constructors

constructor

Usage

val name: String = "Hello World"
var builder: PortalBuilder = PortalBuilder(name)

Parameters

NameTypeDescription
nameStringThe name of the Portal to be referenced via the PortalManager or the PortalView

Returns: PortalBuilder

Methods

setStartDir

Set the directory of the Portal. This directory is the on device directory of where your web application is located.

Usage

var builder: PortalBuilder = someValue
builder = builder.setStartDir("/path/to/web/application/")

Parameters

NameTypeDescription
startDirStringThe on device directory of the web application that this Portal uses.

Returns: PortalBuilder

addPlugin

Add a Capacitor Plugin to be loaded with the Portal being built.

Usage

var builder: PortalBuilder = someValue
builder = builder.addPlugin(MyPlugin::class.java)

Parameters

NameTypeDescription
pluginClass<out Plugin?>A Plugin to be used with the Portal.

Returns: PortalBuilder

setPlugins

Add multiple Capacitor Plugins to be loaded with this Portal.

Usage

var builder: PortalBuilder = someValue
val list: MutableList<Class<out Plugin?}>> = mutableListOf(
FooPlugin::class.java,
BarPlugin::class.java,
BazPlugin::class.java
)
builder = builder.setPlugins(list)

Parameters

NameTypeDescription
pluginsMutableList<Class<out Plugin?>>A list of Plugins to be used with the Portal.

Returns: PortalBuilder

setInitialContext

Sets the initial context to pass to the webview. You can pass in either a Map or a String that will be parsed into a JSON object.

Usage

Map Usage

var builder: PortalBuilder = someValue
val map: Map<String, Any> = mapOf(
"foo" to "bar",
"ionic" to "portals"
"num" to 42
)
builder = builder.setInitialContext(map)

String Usage

var builder: PortalBuilder = someValue
val str: String = '{ "foo": "bar", "ionic": "portals", "num": 42 }'
builder = builder.setInitialContext(str)

In the examples above, your initial context in the web portion of the code will be parsed like this:

interface MyPortalInitialContext {
foo: string;
ionic: string;
num: number;
}

const context = getInitialContext<MyPortalInitialContext>();
console.log(context?.value?.foo); // "bar"
console.log(context?.value?.ionic); // "portals"
console.log(context?.value?.num); // 42

Parameters

NameTypeDescription
initialContextMap<String, Any>A map containing key/pair values that will be converted to a JavaScript object in the webview.

Returns: PortalBuilder

NameTypeDescription
initialContextStringA JSON-valid string that will be converted to a JavaScript object in the webview.

Returns: PortalBuilder

setPortalFragmentType

Sets the PortalFragment class to use when displaying the Portal.

Usage

var builder: PortalBuilder = someValue
var fragmentType: Class<out PortalFragmentType?> = MyPortalFragment::class.java
builder = builder.setPortalFragmentType(list)

Parameters

NameTypeDescription
portalFragmentTypeClass<out PortalFragment?>The class object of type PortalFragment to use for containing the Portal

Returns: PortalBuilder

create

Creates a Portal instance from the current state of PortalBuilder

Usage

var builder: PortalBuilder = someValue
val portal: Portal = builder.create()

Returns: Portal