Shahab R
Shahab R

Reputation: 11

Why do the screen of fragments in my Android app go blank after a few times switching tabs?

The idea behind my app is simple. Just two tabs: A Musical Alarm and a Historical Calendar.

When the user switches back and forth between the tabs using the Bottom Navigation bars, after maybe two or three times switching, the fragments go blank, and the user is presented with an empty screen.

I guess the problem may lie in fragment lifecycles, but not sure.

The full code with issues mentioned are available here:

https://github.com/shahab-devint/musical_alarm_calendar

Any help or insight is much appreciated :)

Upvotes: 0

Views: 23

Answers (1)

Thet Paing Soe
Thet Paing Soe

Reputation: 91

Did you test on emulator or real device ?

When I check your code, I don't encounter that empty screen error. But What I found that is, There is null pointer exception in your onPageFinished callback, when I do quick switch between tabs. This exception happened because onPageFinished callback is called after onDestroyView.

You need to use view?. instead of binding.

Here is the updated code.

override fun onPageFinished(view: WebView?, url: String?) {
    // Add JavaScript interface
    view?.addJavascriptInterface(object : Any() {
        @android.webkit.JavascriptInterface
        fun passPersianDate(date: String) {
            // This method should receive the Persian date from JavaScript
            val persianDate = date
            val dateText = "تاریخ شمسی: $persianDate - Gregorian Date: $gregorianDate"
            binding.dateTextView.text = dateText
        }
    }, "JSInterface")

    // Execute your JavaScript here after the page has loaded
    view?.evaluateJavascript("const options = { year: 'numeric', month: 'long', day: 'numeric' }; const persianDate = new Date().toLocaleDateString('fa-IR', options); JSInterface.passPersianDate(persianDate);", null)
}

Upvotes: 0

Related Questions