Skip to content

Commit b68059e

Browse files
authored
Merge pull request #51 from jagnesh/fix/android/hideSplash_crash
Fix/android/hide splash crash
2 parents edf1e2e + 0b036b0 commit b68059e

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

android/src/main/java/com/splashview/SplashView.kt

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import android.view.WindowManager
1111

1212
object SplashView {
1313
private var splashDialog: Dialog? = null
14-
14+
private var activityRef: java.lang.ref.WeakReference<Any>? = null
1515
fun showSplashView(activity: Activity) {
1616
if (activity.isFinishing || activity.isDestroyed) {
1717
println("Skipping showSplash: Activity is not ready.")
1818
return
1919
}
20+
activityRef = java.lang.ref.WeakReference(activity)
2021

2122
activity.runOnUiThread {
2223
if (splashDialog?.isShowing == true) return@runOnUiThread
@@ -57,7 +58,29 @@ object SplashView {
5758

5859

5960
fun hideSplashView() {
60-
splashDialog?.takeIf { it.isShowing }?.dismiss()
61-
splashDialog = null
61+
val activity = activityRef?.get() as? Activity
62+
63+
if (activity == null) {
64+
splashDialog = null
65+
activityRef = null
66+
return
67+
}
68+
69+
activity.runOnUiThread {
70+
try {
71+
splashDialog?.let { dialog ->
72+
val decorView = dialog.window?.decorView
73+
74+
if (dialog.isShowing && decorView?.isAttachedToWindow == true) {
75+
dialog.dismiss()
76+
}
77+
}
78+
} catch (e: Exception) {
79+
// Safe guard — prevent crash in rare edge cases
80+
} finally {
81+
splashDialog = null
82+
activityRef = null
83+
}
84+
}
6285
}
6386
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-splash-view",
3-
"version": "0.0.19",
3+
"version": "0.0.21",
44
"description": "A lightweight splash screen library for React Native.",
55
"source": "./src/index.tsx",
66
"main": "./lib/commonjs/index.js",

0 commit comments

Comments
 (0)