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.
- Kotlin
- Java
val portal: Portal = PortalBuilder("myPortal")
.addPlugin(MyCapacitorPlugin::class.java)
.setPortalFragmentType(MyFadeInOutPortalFragment::class.java)
.setInitialContext(mapOf("myVariableFromAndroid" to 42))
.setStartDir("web_app")
.create()
Portal portal = new PortalBuilder("myPortal")
.addPlugin(MyCapacitorPlugin.class)
.setPortalFragmentType(MyFadeInOutPortalFragment.class)
.setInitialContext(Map.of("myVariableFromAndroid", 42))
.setStartDir("web_app")
.create();
Constructors
constructor
Usage
- Kotlin
- Java
val name: String = "Hello World"
var builder: PortalBuilder = PortalBuilder(name)
String name = "Hello World"
PortalBuilder builder = new PortalBuilder(name);
Parameters
Name | Type | Description |
---|---|---|
name | String | The 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
- Kotlin
- Java
var builder: PortalBuilder = someValue
builder = builder.setStartDir("/path/to/web/application/")
PortalBuilder builder = someValue;
builder = builder.setStartDir("/path/to/web/application/");
Parameters
Name | Type | Description |
---|---|---|
startDir | String | The 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
- Kotlin
- Java
var builder: PortalBuilder = someValue
builder = builder.addPlugin(MyPlugin::class.java)
PortalBuilder builder = someValue;
builder = builder.addPlugin(MyPlugin.class)
Parameters
Name | Type | Description |
---|---|---|
plugin | Class<out Plugin?> | A Plugin to be used with the Portal. |
Returns: PortalBuilder
setPlugins
Add multiple Capacitor Plugins to be loaded with this Portal.
Usage
- Kotlin
- Java
var builder: PortalBuilder = someValue
val list: MutableList<Class<out Plugin?}>> = mutableListOf(
FooPlugin::class.java,
BarPlugin::class.java,
BazPlugin::class.java
)
builder = builder.setPlugins(list)
PortalBuilder builder = someValue;
List<? extends Plugin> list = Array.asList(
FooPlugin.class,
BarPlugin.class,
BazPlugin.class
);
builder = builder.setPlugins(list);
Parameters
Name | Type | Description |
---|---|---|
plugins | MutableList<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
- Kotlin
- Java
var builder: PortalBuilder = someValue
val map: Map<String, Any> = mapOf(
"foo" to "bar",
"ionic" to "portals"
"num" to 42
)
builder = builder.setInitialContext(map)
PortalBuilder builder = someValue;
Map<String, Object> map = Map.ofEntries(
new AbstractMap.SimpleEntry<String, @NotNull Object>("foo", "bar"),
new AbstractMap.SimpleEntry<String, @NotNull Object>("ionic", "portals"),
new AbstractMap.SimpleEntry<String, @NotNull Object>("num", 42)
);
builder = builder.setInitialContext(map);
String Usage
- Kotlin
- Java
var builder: PortalBuilder = someValue
val str: String = '{ "foo": "bar", "ionic": "portals", "num": 42 }'
builder = builder.setInitialContext(str)
PortalBuilder builder = someValue;
String str = "{ \"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
Name | Type | Description |
---|---|---|
initialContext | Map<String, Any> | A map containing key/pair values that will be converted to a JavaScript object in the webview. |
Returns: PortalBuilder
Name | Type | Description |
---|---|---|
initialContext | String | A 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
- Kotlin
- Java
var builder: PortalBuilder = someValue
var fragmentType: Class<out PortalFragmentType?> = MyPortalFragment::class.java
builder = builder.setPortalFragmentType(list)
PortalBuilder builder = someValue;
Class<? extends PortalFragmentType> fragmentType = MyPortalFragment.class;
builder = builder.setPortalFragmentType(list);
Parameters
Name | Type | Description |
---|---|---|
portalFragmentType | Class<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
- Kotlin
- Java
var builder: PortalBuilder = someValue
val portal: Portal = builder.create()
PortalBuilder builder = someValue;
Portal portal = builder.create();
Returns: Portal