|
| 1 | +import { |
| 2 | + Application, |
| 3 | + setActivityCallbacks, |
| 4 | + AndroidActivityCallbacks, |
| 5 | +} from "@nativescript/core"; |
| 6 | +import { InAppBrowser } from "nativescript-inappbrowser"; |
| 7 | + |
| 8 | +@NativeClass() |
| 9 | +@JavaProxy("org.nativescript.demo.MainActivity") |
| 10 | +export class Activity extends androidx.appcompat.app.AppCompatActivity { |
| 11 | + public isNativeScriptActivity; |
| 12 | + |
| 13 | + private _callbacks: AndroidActivityCallbacks; |
| 14 | + |
| 15 | + public onCreate(savedInstanceState: android.os.Bundle): void { |
| 16 | + Application.android.init(this.getApplication()); |
| 17 | + // Set the isNativeScriptActivity in onCreate (as done in the original NativeScript activity code) |
| 18 | + // The JS constructor might not be called because the activity is created from Android. |
| 19 | + this.isNativeScriptActivity = true; |
| 20 | + if (!this._callbacks) { |
| 21 | + setActivityCallbacks(this); |
| 22 | + } |
| 23 | + |
| 24 | + this._callbacks.onCreate( |
| 25 | + this, |
| 26 | + savedInstanceState, |
| 27 | + this.getIntent(), |
| 28 | + super.onCreate |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + public onNewIntent(intent: android.content.Intent): void { |
| 33 | + this._callbacks.onNewIntent( |
| 34 | + this, |
| 35 | + intent, |
| 36 | + super.setIntent, |
| 37 | + super.onNewIntent |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + public onSaveInstanceState(outState: android.os.Bundle): void { |
| 42 | + this._callbacks.onSaveInstanceState( |
| 43 | + this, |
| 44 | + outState, |
| 45 | + super.onSaveInstanceState |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + public onStart(): void { |
| 50 | + this._callbacks.onStart(this, super.onStart); |
| 51 | + // InAppBrowser initialization (Connect to the Custom Tabs service) |
| 52 | + InAppBrowser.onStart(); |
| 53 | + } |
| 54 | + |
| 55 | + public onStop(): void { |
| 56 | + this._callbacks.onStop(this, super.onStop); |
| 57 | + } |
| 58 | + |
| 59 | + public onDestroy(): void { |
| 60 | + this._callbacks.onDestroy(this, super.onDestroy); |
| 61 | + } |
| 62 | + |
| 63 | + public onPostResume(): void { |
| 64 | + this._callbacks.onPostResume(this, super.onPostResume); |
| 65 | + } |
| 66 | + |
| 67 | + public onBackPressed(): void { |
| 68 | + this._callbacks.onBackPressed(this, super.onBackPressed); |
| 69 | + } |
| 70 | + |
| 71 | + public onRequestPermissionsResult( |
| 72 | + requestCode: number, |
| 73 | + permissions: Array<string>, |
| 74 | + grantResults: Array<number> |
| 75 | + ): void { |
| 76 | + this._callbacks.onRequestPermissionsResult( |
| 77 | + this, |
| 78 | + requestCode, |
| 79 | + permissions, |
| 80 | + grantResults, |
| 81 | + undefined /*TODO: Enable if needed*/ |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + public onActivityResult( |
| 86 | + requestCode: number, |
| 87 | + resultCode: number, |
| 88 | + data: android.content.Intent |
| 89 | + ): void { |
| 90 | + this._callbacks.onActivityResult( |
| 91 | + this, |
| 92 | + requestCode, |
| 93 | + resultCode, |
| 94 | + data, |
| 95 | + super.onActivityResult |
| 96 | + ); |
| 97 | + } |
| 98 | +} |
0 commit comments