Starting an Activity and finishing the previous Activity when using Material Transitions shows Home screen (finishAfterTransition not working)

Viewed 167

I am trying to start a Activity B after logging in in Activity A with Material transitions (specifically Fade). Android provides a finishAfterTransition() but this does not do what it says on the tin (Google seem to know this, and have been ignoring the issue for years) and many other people seem to complain about the issue. Therefore, whenever starting an activity and finishing the previous one, I always see the home page briefly because the old activity is finished before the new one is started. I thought about using a BroadcastReceiver and finishing Activity A from Activity B - but this goes against Activity behaviour. I also had a feeling running start and finish synchronously might work, but the thread just hangs on the startActivity (runBlocking dispatched to Main thread).

Does anyone have a nice, elegant work around solution for this, so I can make proper use of the Material Transitions that Android provide (but make it very difficult to use) and the navigation in my app between activities just looks pretty awful. Here's some sample code (the origin here is my Activity A):

if (params.finishOrigin) {
            params.origin.finishAfterTransition().apply {
                if (animateDialog) {
                    with(params.origin.window) {
                        enterTransition = Fade().apply {
                            duration = 200L
                        }
                        exitTransition = Fade().apply {
                            duration = 200L
                        }
                    }
                } else if (animate) {
                    params.origin.overridePendingTransition(
                        params.finishAnimationIn,
                        params.finishAnimationOut
                    )
                }
            }
        }

        params.origin.startActivity(
            Intent(params.origin, target).apply {
                if (params.finishOrigin) {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
                }
                data = params.originalIntent?.data
                params.block(this)
                if (animateDialog) {
                    with(params.origin.window) {
                        enterTransition = Fade().apply {
                            duration = 1200L
                        }
                        exitTransition = Fade().apply {
                            duration = 200L
                        }
                    }
                } else if (animate) {
                    params.origin.overridePendingTransition(
                        params.originAnimationIn,
                        params.originAnimationOut
                    )
                }
            },
            if (animateDialog) ActivityOptions.makeSceneTransitionAnimation(params.origin).toBundle() else null
        )

NOTE: The ordering of the start/finish has no effect, since they're called asynchronously.

I have also used different animations in overridePendingTransition but always see a white screen/flicker with any animation other than left-to-right slide in. These Material transitions actually look pretty nice as well, if they were even compatible with basic navigational tasks in Android.

0 Answers
Related