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

Drawer doesn't appear when using content.viewTreeObserver.addOnPreDrawListener on the Acitivty #107

Open
rahulsainani opened this issue Jul 13, 2022 · 5 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@rahulsainani
Copy link
Contributor

rahulsainani commented Jul 13, 2022

On adding the SplashScreenApi and keeping it visible while some background tasks are happening as shared in the docs, the drawer doesn't appear. Is there a way we can work around this?

@pandulapeter pandulapeter self-assigned this Jul 14, 2022
@pandulapeter
Copy link
Owner

Hi!

The splash screen itself is not an Activity as far as I know, so there is no way to add the debug menu on top of it. In that state the Application class already exists, but the first frame of the first Activity is not yet drawn (the layout is not yet inflated). Delaying that state using a PreDrawListener simply keeps the Splash UI visible: you won't see Beagle until the Activity actually becomes visible. However, once your first Activity is drawn, Beagle should be there.

Sorry, let me think about this a bit more, but adding custom UI like a debug menu to the splash screen should be impossible, the way I understand it (rightfully so: the splash is there to be a lightweight placeholder that can be shown while the more heavy stuff is getting loaded into memory).

One option (that I personally dislike) is to add a SplashActivity that looks just like your splash screen. But that way at least you could do error handling in case your background tasks fail.

@rahulsainani
Copy link
Contributor Author

Sorry, my message wasn't framed correctly, I should have added that:
We have a single activity and we initialise the debug drawer in the application, but since adding the PreDrawListener, the debug drawer doesn't appear on that activity. We still want to debug drawer on the activity, not the splash screen. When using just the splash screen api without the PreDrawListener, the debug drawer works fine.

@pandulapeter
Copy link
Owner

Thanks for clarifying. Let me look into it more, now I get it that it's a bug that should be fixed.

@pandulapeter pandulapeter added the bug Something isn't working label Jul 15, 2022
@pandulapeter
Copy link
Owner

Hey @rahulsainani !

Sorry for taking this long. Unfortunately I'm having trouble reproducing the issue.

I'm using the latest version of the splashscreen dependency, 1.0.0.

I modified the demo app in this repository. It has a single Activity, which does the following in onCreate():

  • Invokes the super.onCreate() implementation.
  • Calls the installSplashScreen() function.
  • Inflates the layout of the Activity.
  • Adds the following piece of code to delay the splash screen:
var shouldShowSplashScreen = true
lifecycleScope.launch {
    delay(3000)
    shouldShowSplashScreen = false
}
val content: View = findViewById(android.R.id.content)
content.viewTreeObserver.addOnPreDrawListener(
    object : ViewTreeObserver.OnPreDrawListener {
        override fun onPreDraw() = if (shouldShowSplashScreen) false else {
            content.viewTreeObserver.removeOnPreDrawListener(this)
            true
        }
    }
)

With these changes, the splash screen is kept visible for an extra 3 seconds, as expected. Also, when it disappears, the DrawerLayout of the debug menu works as expected. Am I missing something? Can you please double-check?

@pandulapeter pandulapeter added the question Further information is requested label Jul 31, 2022
@rahulsainani
Copy link
Contributor Author

Hi @pandulapeter Sorry for the delay on my side. Switched project.

I tried to use the same code and realised that the error happens because we call SetContent inside onPreDraw. Here's the code snippet:

private fun setupPreDrawListener(onAuthCheckComplete: (Intent?) -> Unit) {
        Logger.d { "setupPreDrawListener" }
        val content: View = findViewById(android.R.id.content)
        content.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
            override fun onPreDraw(): Boolean {
                Logger.d { "onPreDraw" }
                val value = viewModel.startDestinationState.value
                return if (value != null) {
                    Logger.d { "Will remove pre draw listener" }
                    content.viewTreeObserver.removeOnPreDrawListener(this)
                    onAuthCheckComplete(mapToIntent(value))
                    true
                } else {
                    Logger.d { "Continue checking" }
                    false
                }
            }
        })
    }

And onCreate after splash setup looks like:

setupPreDrawListener(onAuthCheckComplete = { intent ->
            Logger.d { "onAuthCheckComplete: $intent" }
            setContent {
                App(deepLinkIntent = intent)
            }
        })

on moving SetContent outside of onAuthCheckComplete callback, to onCreate, the debug drawer works fine. I guess the issue is how we are hacking this.

If you think this isn't a bug, please feel free to close this. If you have any ideas around why this is happening, it would be awesome if you could share it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants