Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for DEV-1351 #198

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ internal class LatteFragment() : Fragment() {
get() = mutWebView!!
set(value) { mutWebView = value }

// This flag is used to work around an issue of fragment re-creation.
// When this fragment is used with androidx.navigation,
// this fragment may be onDestroyView(), and then onCreateView() again.
// If that happens, the webView may never has its load() called.
// However, even with this fix, savedInstanceState was never called, so
// the webView will show the initial screen.
private var waitWebViewToLoadIsCalledInThisLifecycle: Boolean = false
Comment on lines +90 to +96
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it means sometimes onDestroyView will be called but onSaveInstanceState won't call?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://developer.android.com/guide/fragments/lifecycle#states
onSaveInstanceState is always called before calling onDestroyView
So on onCreateView the webview should be able to restore the state 🤔


private var webViewOnReady: (() -> Unit)? = null
private var webViewOnComplete: ((result: Result<WebViewResult>) -> Unit)? = null

Expand Down Expand Up @@ -188,6 +196,7 @@ internal class LatteFragment() : Fragment() {
}
this@LatteFragment.webViewOnReady = webViewOnReady
this@LatteFragment.webViewOnComplete = webViewOnComplete
this@LatteFragment.waitWebViewToLoadIsCalledInThisLifecycle = true
webView.load()
}
}
Expand Down Expand Up @@ -311,7 +320,15 @@ internal class LatteFragment() : Fragment() {
val webViewStateBundle = savedInstanceState?.getBundle(KEY_WEBVIEW_STATE)
val ctx = requireContext()
constructWebViewIfNeeded(ctx, webViewStateBundle)
removeWebViewFromParent(webView)

// This lifecycle started.
// If waitWebViewToLoad is called, then waitWebViewToLoadIsCalledInThisLifecycle is true.
// if waitWebViewToLoadIsCalledInThisLifecycle is true, then webView.load() was called there.
// Otherwise, we have to call webView.load() here.
if (!waitWebViewToLoadIsCalledInThisLifecycle) {
webView.load()
}

val intentFilter = IntentFilter(Latte.BroadcastType.RESET_PASSWORD_COMPLETED.action)
if (Build.VERSION.SDK_INT >= 33) {
ctx.registerReceiver(broadcastReceiver, intentFilter, Context.RECEIVER_NOT_EXPORTED)
Expand All @@ -326,6 +343,11 @@ internal class LatteFragment() : Fragment() {

override fun onDestroyView() {
super.onDestroyView()

// This lifecycle ended.
// Reset waitWebViewToLoadIsCalledInThisLifecycle to false.
waitWebViewToLoadIsCalledInThisLifecycle = false

removeWebViewFromParent(webView)
mutWebView = null
val ctx = requireContext()
Expand Down
Loading