Skip to content

Commit f95b86d

Browse files
committed
Fix lifecycle detection
1 parent 6ffb0fe commit f95b86d

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

ui/src/main/java/io/snabble/sdk/ui/payment/PayoneInputView.kt

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import java.nio.charset.Charset
4040
import java.text.NumberFormat
4141
import java.util.*
4242

43-
class PayoneInputView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) {
43+
class PayoneInputView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr), LifecycleObserver {
4444
private var acceptedKeyguard = false
4545
private lateinit var webView: WebView
4646
private lateinit var progressBar: ProgressBar
@@ -117,7 +117,7 @@ class PayoneInputView @JvmOverloads constructor(context: Context, attrs: Attribu
117117
description: String,
118118
failingUrl: String
119119
) {
120-
Logger.d("onReceivedError $failingUrl")
120+
Logger.e("onReceivedError $failingUrl")
121121
Dispatch.mainThread { finishWithError() }
122122
}
123123

@@ -327,22 +327,26 @@ class PayoneInputView @JvmOverloads constructor(context: Context, attrs: Attribu
327327

328328
public override fun onAttachedToWindow() {
329329
super.onAttachedToWindow()
330-
val fragmentActivity = UIUtils.getHostFragmentActivity(context)
331-
fragmentActivity?.lifecycle?.addObserver(object : LifecycleObserver {
332-
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
333-
fun onResume() {
334-
isActivityResumed = true
335-
if (this@PayoneInputView::creditCardInfo.isInitialized) polling.doNow()
336-
}
330+
isActivityResumed = UIUtils.getHostFragmentActivity(context)?.lifecycle?.let { lifecycle ->
331+
lifecycle.addObserver(this)
332+
lifecycle.currentState.isAtLeast(Lifecycle.State.RESUMED)
333+
} ?: true
334+
}
337335

338-
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
339-
fun onPause() {
340-
isActivityResumed = false
341-
fragmentActivity.lifecycle.removeObserver(this)
342-
}
343-
})
344-
isActivityResumed =
345-
fragmentActivity?.lifecycle?.currentState?.isAtLeast(Lifecycle.State.RESUMED) ?: true
336+
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
337+
fun onResume() {
338+
isActivityResumed = true
339+
if (this@PayoneInputView::creditCardInfo.isInitialized) polling.doNow()
340+
}
341+
342+
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
343+
fun onPause() {
344+
isActivityResumed = false
345+
}
346+
347+
override fun onDetachedFromWindow() {
348+
super.onDetachedFromWindow()
349+
UIUtils.getHostFragmentActivity(context)?.lifecycle?.removeObserver(this)
346350
}
347351

348352
private data class CreditCardInfo(

0 commit comments

Comments
 (0)