| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4cf8a15 commit 304067f
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,7 +41,8 @@ if (app.android) { | |||
| 41 | 41 | export var testAndroidApplicationInitialized = function () { | |
| 42 | 42 | TKUnit.assert(app.android, "Android application not initialized."); | |
| 43 | 43 | TKUnit.assert(app.android.context, "Android context not initialized."); | |
| 44 | - TKUnit.assert(app.android.foregroundActivity, "Android currentActivity not initialized."); | ||
| 44 | + TKUnit.assert(app.android.currentContext, "Android currentContext not initialized."); | ||
| 45 | + TKUnit.assert(app.android.foregroundActivity, "Android foregroundActivity not initialized."); | ||
| 45 | 46 | TKUnit.assert(app.android.startActivity, "Android startActivity not initialized."); | |
| 46 | 47 | TKUnit.assert(app.android.nativeApp, "Android nativeApp not initialized."); | |
| 47 | 48 | TKUnit.assert(app.android.packageName, "Android packageName not initialized."); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,16 +4,16 @@ import frame = require("ui/frame"); | |||
| 4 | 4 | import observable = require("data/observable"); | |
| 5 | 5 | import * as typesModule from "utils/types"; | |
| 6 | 6 | import * as enumsModule from "ui/enums"; | |
| 7 | + import { isNativeScriptActivity } from "ui/frame/activity"; | ||
| 7 | 8 | let enums: typeof enumsModule; | |
| 8 | 9 | ||
| 9 | 10 | global.moduleMerge(appModule, exports); | |
| 10 | - var typedExports: typeof definition = exports; | ||
| 11 | + const typedExports: typeof definition = exports; | ||
| 11 | 12 | ||
| 12 | 13 | function initLifecycleCallbacks() { | |
| 13 | 14 | // TODO: Verify whether the logic for triggerring application-wide events based on Activity callbacks is working properly | |
| 14 | 15 | let lifecycleCallbacks = new android.app.Application.ActivityLifecycleCallbacks({ | |
| 15 | 16 | onActivityCreated: function (activity: any, bundle: any) { | |
| 16 | - | ||
| 17 | 17 | // Set app theme after launch screen was used during startup | |
| 18 | 18 | let activityInfo = activity.getPackageManager().getActivityInfo(activity.getComponentName(), android.content.pm.PackageManager.GET_META_DATA); | |
| 19 | 19 | if (activityInfo.metaData) { | |
@@ -25,38 +25,20 @@ function initLifecycleCallbacks() { | |||
| 25 | 25 | ||
| 26 | 26 | if (!androidApp.startActivity) { | |
| 27 | 27 | androidApp.startActivity = activity; | |
| 28 | - androidApp.notify(<definition.AndroidActivityBundleEventData>{ eventName: "activityCreated", object: androidApp, activity: activity, bundle: bundle }); | ||
| 29 | - | ||
| 30 | - if (androidApp.onActivityCreated) { | ||
| 31 | - androidApp.onActivityCreated(activity, bundle); | ||
| 32 | - } | ||
| 33 | 28 | } | |
| 34 | 29 | ||
| 35 | - androidApp.currentContext = activity; | ||
| 30 | + androidApp.notify(<definition.AndroidActivityBundleEventData>{ eventName: "activityCreated", object: androidApp, activity: activity, bundle: bundle }); | ||
| 31 | + if (androidApp.onActivityCreated) { | ||
| 32 | + androidApp.onActivityCreated(activity, bundle); | ||
| 33 | + } | ||
| 36 | 34 | }, | |
| 37 | 35 | ||
| 38 | 36 | onActivityDestroyed: function (activity: any) { | |
| 39 | - // Clear the current activity reference to prevent leak | ||
| 40 | - if (activity === androidApp.foregroundActivity) { | ||
| 41 | - androidApp.foregroundActivity = undefined; | ||
| 42 | - } | ||
| 43 | - | ||
| 44 | - if (activity === androidApp.currentContext) { | ||
| 45 | - androidApp.currentContext = undefined; | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | 37 | if (activity === androidApp.startActivity) { | |
| 49 | - if (typedExports.onExit) { | ||
| 50 | - typedExports.onExit(); | ||
| 51 | - } | ||
| 52 | - | ||
| 53 | - typedExports.notify(<definition.ApplicationEventData>{ eventName: typedExports.exitEvent, object: androidApp, android: activity }); | ||
| 54 | - | ||
| 55 | 38 | androidApp.startActivity = undefined; | |
| 56 | 39 | } | |
| 57 | 40 | ||
| 58 | 41 | androidApp.notify(<definition.AndroidActivityEventData>{ eventName: "activityDestroyed", object: androidApp, activity: activity }); | |
| 59 | - | ||
| 60 | 42 | if (androidApp.onActivityDestroyed) { | |
| 61 | 43 | androidApp.onActivityDestroyed(activity); | |
| 62 | 44 | } | |
@@ -66,35 +48,38 @@ function initLifecycleCallbacks() { | |||
| 66 | 48 | }, | |
| 67 | 49 | ||
| 68 | 50 | onActivityPaused: function (activity: any) { | |
| 69 | - androidApp.paused = true; | ||
| 70 | - | ||
| 71 | 51 | if (activity === androidApp.foregroundActivity) { | |
| 52 | + androidApp.foregroundActivity = undefined; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + if (isNativeScriptActivity(activity)) { | ||
| 56 | + androidApp.paused = true; | ||
| 57 | + | ||
| 72 | 58 | if (typedExports.onSuspend) { | |
| 73 | 59 | typedExports.onSuspend(); | |
| 74 | 60 | } | |
| 75 | - | ||
| 76 | 61 | typedExports.notify(<definition.ApplicationEventData>{ eventName: typedExports.suspendEvent, object: androidApp, android: activity }); | |
| 77 | 62 | } | |
| 78 | 63 | ||
| 79 | 64 | androidApp.notify(<definition.AndroidActivityEventData>{ eventName: "activityPaused", object: androidApp, activity: activity }); | |
| 80 | - | ||
| 81 | 65 | if (androidApp.onActivityPaused) { | |
| 82 | 66 | androidApp.onActivityPaused(activity); | |
| 83 | 67 | } | |
| 84 | 68 | }, | |
| 85 | 69 | ||
| 86 | 70 | onActivityResumed: function (activity: any) { | |
| 87 | - androidApp.paused = false; | ||
| 88 | 71 | androidApp.foregroundActivity = activity; | |
| 89 | 72 | ||
| 90 | - if (typedExports.onResume) { | ||
| 91 | - typedExports.onResume(); | ||
| 92 | - } | ||
| 73 | + if (isNativeScriptActivity(activity)) { | ||
| 74 | + if (typedExports.onResume) { | ||
| 75 | + typedExports.onResume(); | ||
| 76 | + } | ||
| 77 | + typedExports.notify(<definition.ApplicationEventData>{ eventName: typedExports.resumeEvent, object: androidApp, android: activity }); | ||
| 93 | 78 | ||
| 94 | - typedExports.notify(<definition.ApplicationEventData>{ eventName: typedExports.resumeEvent, object: androidApp, android: activity }); | ||
| 79 | + androidApp.paused = false; | ||
| 80 | + } | ||
| 95 | 81 | ||
| 96 | 82 | androidApp.notify(<definition.AndroidActivityEventData>{ eventName: "activityResumed", object: androidApp, activity: activity }); | |
| 97 | - | ||
| 98 | 83 | if (androidApp.onActivityResumed) { | |
| 99 | 84 | androidApp.onActivityResumed(activity); | |
| 100 | 85 | } | |
@@ -109,18 +94,14 @@ function initLifecycleCallbacks() { | |||
| 109 | 94 | }, | |
| 110 | 95 | ||
| 111 | 96 | onActivityStarted: function (activity: any) { | |
| 112 | - androidApp.foregroundActivity = activity; | ||
| 113 | - | ||
| 114 | 97 | androidApp.notify(<definition.AndroidActivityEventData>{ eventName: "activityStarted", object: androidApp, activity: activity }); | |
| 115 | - | ||
| 116 | 98 | if (androidApp.onActivityStarted) { | |
| 117 | 99 | androidApp.onActivityStarted(activity); | |
| 118 | 100 | } | |
| 119 | 101 | }, | |
| 120 | 102 | ||
| 121 | 103 | onActivityStopped: function (activity: any) { | |
| 122 | 104 | androidApp.notify(<definition.AndroidActivityEventData>{ eventName: "activityStopped", object: androidApp, activity: activity }); | |
| 123 | - | ||
| 124 | 105 | if (androidApp.onActivityStopped) { | |
| 125 | 106 | androidApp.onActivityStopped(activity); | |
| 126 | 107 | } | |
@@ -195,12 +176,15 @@ export class AndroidApplication extends observable.Observable implements definit | |||
| 195 | 176 | public paused: boolean; | |
| 196 | 177 | public nativeApp: android.app.Application; | |
| 197 | 178 | public context: android.content.Context; | |
| 198 | - public currentContext: android.content.Context; | ||
| 199 | 179 | public foregroundActivity: android.app.Activity; | |
| 200 | 180 | public startActivity: android.app.Activity; | |
| 201 | 181 | public packageName: string; | |
| 202 | 182 | public hasActionBar: boolean; | |
| 203 | 183 | ||
| 184 | + public get currentContext(): android.content.Context { | ||
| 185 | + return this.foregroundActivity; | ||
| 186 | + } | ||
| 187 | + | ||
| 204 | 188 | public onActivityCreated: (activity: android.app.Activity, bundle: android.os.Bundle) => void; | |
| 205 | 189 | ||
| 206 | 190 | public onActivityDestroyed: (activity: android.app.Activity) => void; | |
@@ -219,7 +203,7 @@ export class AndroidApplication extends observable.Observable implements definit | |||
| 219 | 203 | ||
| 220 | 204 | public init(nativeApp: any) { | |
| 221 | 205 | if (this.nativeApp) { | |
| 222 | - throw new Error("application.android already initialized.") | ||
| 206 | + throw new Error("application.android already initialized."); | ||
| 223 | 207 | } | |
| 224 | 208 | ||
| 225 | 209 | this.nativeApp = nativeApp; | |
@@ -238,10 +222,10 @@ export class AndroidApplication extends observable.Observable implements definit | |||
| 238 | 222 | private _pendingReceiverRegistrations = new Array<(context: android.content.Context) => void>(); | |
| 239 | 223 | private _registerPendingReceivers() { | |
| 240 | 224 | if (this._pendingReceiverRegistrations) { | |
| 241 | - var i = 0; | ||
| 242 | - var length = this._pendingReceiverRegistrations.length; | ||
| 225 | + let i = 0; | ||
| 226 | + const length = this._pendingReceiverRegistrations.length; | ||
| 243 | 227 | for (; i < length; i++) { | |
| 244 | - var registerFunc = this._pendingReceiverRegistrations[i]; | ||
| 228 | + const registerFunc = this._pendingReceiverRegistrations[i]; | ||
| 245 | 229 | registerFunc(this.context); | |
| 246 | 230 | } | |
| 247 | 231 | this._pendingReceiverRegistrations = new Array<(context: android.content.Context) => void>(); | |
@@ -250,12 +234,12 @@ export class AndroidApplication extends observable.Observable implements definit | |||
| 250 | 234 | ||
| 251 | 235 | public registerBroadcastReceiver(intentFilter: string, onReceiveCallback: (context: android.content.Context, intent: android.content.Intent) => void) { | |
| 252 | 236 | ensureBroadCastReceiverClass(); | |
| 253 | - var that = this; | ||
| 254 | - var registerFunc = function (context: android.content.Context) { | ||
| 255 | - var receiver: android.content.BroadcastReceiver = new BroadcastReceiverClass(onReceiveCallback); | ||
| 237 | + const that = this; | ||
| 238 | + const registerFunc = function (context: android.content.Context) { | ||
| 239 | + const receiver: android.content.BroadcastReceiver = new BroadcastReceiverClass(onReceiveCallback); | ||
| 256 | 240 | context.registerReceiver(receiver, new android.content.IntentFilter(intentFilter)); | |
| 257 | 241 | that._registeredReceivers[intentFilter] = receiver; | |
| 258 | - } | ||
| 242 | + }; | ||
| 259 | 243 | ||
| 260 | 244 | if (this.context) { | |
| 261 | 245 | registerFunc(this.context); | |
@@ -266,7 +250,7 @@ export class AndroidApplication extends observable.Observable implements definit | |||
| 266 | 250 | } | |
| 267 | 251 | ||
| 268 | 252 | public unregisterBroadcastReceiver(intentFilter: string) { | |
| 269 | - var receiver = this._registeredReceivers[intentFilter]; | ||
| 253 | + const receiver = this._registeredReceivers[intentFilter]; | ||
| 270 | 254 | if (receiver) { | |
| 271 | 255 | this.context.unregisterReceiver(receiver); | |
| 272 | 256 | this._registeredReceivers[intentFilter] = undefined; | |
@@ -350,15 +334,15 @@ global.__onLiveSync = function () { | |||
| 350 | 334 | appModule.__onLiveSync(); | |
| 351 | 335 | ||
| 352 | 336 | loadCss(); | |
| 353 | - } | ||
| 337 | + }; | ||
| 354 | 338 | ||
| 355 | 339 | global.__onUncaughtError = function (error: definition.NativeScriptError) { | |
| 356 | - var types: typeof typesModule = require("utils/types"); | ||
| 340 | + const types: typeof typesModule = require("utils/types"); | ||
| 357 | 341 | ||
| 358 | 342 | // TODO: Obsolete this | |
| 359 | 343 | if (types.isFunction(typedExports.onUncaughtError)) { | |
| 360 | 344 | typedExports.onUncaughtError(error); | |
| 361 | 345 | } | |
| 362 | 346 | ||
| 363 | 347 | typedExports.notify({ eventName: typedExports.uncaughtErrorEvent, object: appModule.android, android: error }); | |
| 364 | - } | ||
| 348 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -358,12 +358,12 @@ declare module "application" { | |||
| 358 | 358 | foregroundActivity: any /* android.app.Activity */; | |
| 359 | 359 | ||
| 360 | 360 | /** | |
| 361 | - * The currently active (loaded) Context. This is typically the top-level Activity that is just created. | ||
| 361 | + * [Deprecated. Please use the respective event instead.] Please use foregroundActivity property. | ||
| 362 | 362 | */ | |
| 363 | 363 | currentContext: any /* android.content.Context */; | |
| 364 | 364 | ||
| 365 | 365 | /** | |
| 366 | - * The main (start) Activity for the application. | ||
| 366 | + * [Deprecated. Please use foregroundActivity or activity related events instead.] The main (start) Activity for the application. | ||
| 367 | 367 | */ | |
| 368 | 368 | startActivity: any /* android.app.Activity */; | |
| 369 | 369 | ||
@@ -373,7 +373,7 @@ declare module "application" { | |||
| 373 | 373 | packageName: string; | |
| 374 | 374 | ||
| 375 | 375 | /** | |
| 376 | - * True if the application is not running (suspended), false otherwise. | ||
| 376 | + * True if the main application activity is not running (suspended), false otherwise. | ||
| 377 | 377 | */ | |
| 378 | 378 | paused: boolean; | |
| 379 | 379 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments