{"record":{"id":"2dad549543810426","repo":"NativeScript/NativeScript","slug":"nativewindow-activity-is-no-longer-available","errorCode":null,"errorMessage":"NativeWindow: Activity is no longer available.","messagePattern":"NativeWindow: Activity is no longer available\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/native-window/native-window.android.ts","lineNumber":104,"sourceCode":"\tget activity(): androidx.appcompat.app.AppCompatActivity | undefined {\n\t\treturn this._activity?.deref();\n\t}\n\n\tget android() {\n\t\tconst activity = this.activity;\n\t\tif (!activity) {\n\t\t\treturn undefined;\n\t\t}\n\t\treturn { activity };\n\t}\n\n\t/**\n\t * Platform-specific: apply the view as root content of this Activity.\n\t */\n\tprotected _setNativeContent(view: View): void {\n\t\tconst activity = this.activity;\n\t\tif (!activity) {\n\t\t\tthrow new Error('NativeWindow: Activity is no longer available.');\n\t\t}\n\n\t\tconst callbacks: AndroidActivityCallbacks = (activity as any)[CALLBACKS];\n\t\tif (!callbacks) {\n\t\t\tthrow new Error('NativeWindow: Cannot find activity callbacks.');\n\t\t}\n\t\tcallbacks.resetActivityContent(activity, view);\n\t}\n\n\t/**\n\t * Close this window by finishing the activity.\n\t */\n\tclose(): void {\n\t\tif (this.isPrimary) {\n\t\t\tconsole.log('NativeWindow: Cannot close the primary window.');\n\t\t\treturn;\n\t\t}\n","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/NativeScript/NativeScript/blob/6800aefa6511d23ddad8beb38ccc8541c5e91628/packages/core/native-window/native-window.android.ts#L86-L122","documentation":"The Android NativeWindow applies content by setting it as the root view of its backing Activity. If this.activity is null/falsy - meaning the Activity reference is gone (app relaunched, activity destroyed after backgrounding, or window created without a live activity) - _setNativeContent() throws because there is no native window to attach the view to.","triggerScenarios":"Calling setContent()/open() on a NativeWindow after its Activity has been destroyed (e.g. after process death and recreation, or on Android when the app was backgrounded and the activity finished).","commonSituations":"Restoring UI from saved state after Android kills the activity; holding a NativeWindow reference across configuration changes (rotation); delayed callbacks/timers firing after activity teardown.","solutions":["Recreate the NativeWindow (or re-resolve its activity) before setting content on Android.","Check window.activity (or an isAlive/isOpen check) before calling setContent().","Cancel pending async work/timers that reference the window when the activity is destroyed.","Listen for application lifecycle events (suspend/resume) to refresh window references."],"exampleFix":"// before\npendingWindow.setContent(newView); // activity already destroyed\n// after\nif (pendingWindow && pendingWindow.activity) {\n  pendingWindow.setContent(newView);\n}","handlingStrategy":"validation","validationCode":"if (!win || !win.activity) {\n  win = recreateNativeWindow(); // activity gone; rebuild before setting content\n}\nwin._setNativeContent(view);","typeGuard":"function canApplyNativeContent(win) {\n  return !!win && typeof win.activity === 'object' && win.activity !== null && !win.activity.isFinishing();\n}","tryCatchPattern":"try {\n  win.setContent(view);\n} catch (e) {\n  if (String(e.message).includes('Activity is no longer available')) {\n    win = recreateNativeWindow();\n    win.setContent(view);\n  } else throw e;\n}","preventionTips":["Don't cache NativeWindow references across activity destruction/recreation.","Cancel timers/async tasks on application suspend.","Check activity availability before every Android-specific native-window call."],"tags":["native-window","android","activity-lifecycle"],"backgroundTag":"activity-unavailable","analyzedSha":"6800aefa6511d23ddad8beb38ccc8541c5e91628","analyzedAt":"2026-08-30T20:04:56.809Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}