From d1be25f615339ec9e3f0fc0379adafcb51358ca0 Mon Sep 17 00:00:00 2001 From: Tuomas Airaksinen Date: Thu, 18 Jan 2024 16:49:51 +0200 Subject: [PATCH] Fix #2030 (or try to). Crash - BadTokenException unable to add window --- app/src/main/java/net/bible/android/BibleApplication.kt | 7 ++++++- .../java/net/bible/android/control/event/ToastEvent.kt | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/net/bible/android/BibleApplication.kt b/app/src/main/java/net/bible/android/BibleApplication.kt index 440810f7a3..81ed1bdf49 100644 --- a/app/src/main/java/net/bible/android/BibleApplication.kt +++ b/app/src/main/java/net/bible/android/BibleApplication.kt @@ -18,6 +18,7 @@ package net.bible.android import android.annotation.SuppressLint +import android.app.Activity import android.app.Application import android.app.Notification import android.app.NotificationChannel @@ -41,7 +42,9 @@ import net.bible.android.control.backup.BackupControl import net.bible.android.control.event.ABEventBus import net.bible.android.control.event.ToastEvent import net.bible.android.control.report.BugReport +import net.bible.android.view.activity.base.CurrentActivityHolder import net.bible.android.view.activity.base.ErrorActivity +import net.bible.android.view.activity.page.currentActivity import net.bible.android.view.util.locale.LocaleHelper import net.bible.service.cloudsync.SYNC_NOTIFICATION_CHANNEL import net.bible.service.common.BuildVariant @@ -267,8 +270,10 @@ open class BibleApplication : Application() { fun onEventMainThread(ev: ToastEvent) { val duration = ev.duration ?: Toast.LENGTH_SHORT val message = if (ev.messageId != null) getString(ev.messageId) else ev.message + val context = ev.context ?: CurrentActivityHolder.currentActivity?: return + if ((context as? Activity)?.isFinishing == true) return try { - Toast.makeText(this, message, duration).show() + Toast.makeText(context, message, duration).show() } catch (e: Exception) { Log.e(TAG, "Error in showing toast $message", e) } diff --git a/app/src/main/java/net/bible/android/control/event/ToastEvent.kt b/app/src/main/java/net/bible/android/control/event/ToastEvent.kt index 72aedc7f56..e9749030dd 100644 --- a/app/src/main/java/net/bible/android/control/event/ToastEvent.kt +++ b/app/src/main/java/net/bible/android/control/event/ToastEvent.kt @@ -17,7 +17,9 @@ package net.bible.android.control.event -class ToastEvent(val message: String = "", val duration: Int? = null, val messageId: Int? = null) { +import android.content.Context + +class ToastEvent(val message: String = "", val duration: Int? = null, val messageId: Int? = null, val context: Context? = null) { constructor(message: String) : this(message, null) constructor(messageId: Int) : this("", null, messageId) }