VueNativeActivity
VueNativeActivity is the base AppCompatActivity for Vue Native apps on Android. Subclass it to get the JS runtime, native bridge, and hot reload wired up automatically.
Usage
import com.vuenative.core.NativeModule
import com.vuenative.core.VueNativeActivity
class MainActivity : VueNativeActivity() {
// Required: path to the JS bundle in the app's assets folder
override fun getBundleAssetPath() = "vue-native-bundle.js"
// Optional: WebSocket URL for hot reload (development only)
override fun getDevServerUrl(): String? {
return if (BuildConfig.DEBUG) {
// 10.0.2.2 = host machine from Android emulator
// Use your computer's local IP for a real device
"ws://10.0.2.2:8174"
} else null
}
// Optional: create app-owned modules for this JavaScript world
override fun createNativeModules(): List<NativeModule> = listOf(MyModule())
}
Methods
getBundleAssetPath(): String (abstract)
Return the path to the JS bundle in your app's assets/ folder.
getDevServerUrl(): String? (open)
Return the WebSocket URL of the Vite dev server. Return null (default) to disable hot reload and load only from assets.
createNativeModules(): List<NativeModule> (protected open)
Return application-specific native modules. The Activity calls this factory at startup and again for every accepted hot reload, so return new module instances on every call. The previous JavaScript world's modules are destroyed before their replacements are initialized.
Application modules are registered after the built-in and generated modules. If an application module uses the same moduleName as an earlier module, the application module intentionally replaces that implementation.
What it does
VueNativeActivity handles:
- Creates a
FrameLayoutroot container that fills the screen - Sets up edge-to-edge rendering (draws behind status and navigation bars)
- Initializes
JSRuntime(V8 engine, polyfills onVueNative-JSthread) - Initializes
NativeBridgewith the root container - Registers built-in modules, generated modules, then application modules from
createNativeModules() - Wires up
__VN_resolveCallbackand__VN_handleEventrouting - Loads the JS bundle from assets (or connects hot reload)
Permissions
VueNativeActivity implements onRequestPermissionsResult and forwards it to PermissionsModule. If you override this method, call super first:
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
// your code
}
Back button
Pressing the system back button dispatches the hardware:backPress global event. Use useBackHandler() to consume it from a component:
import { useBackHandler } from '@thelacanians/vue-native-runtime'
useBackHandler(() => {
router.pop()
return true
})
Return true after handling the action. Return false to run the default Android back action, which finishes the Activity. If JavaScript has no hardware:backPress listener, VueNativeActivity also performs that default action.