You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shared Element Transition失效:TextView跨Activity动画无效且文本消失求助

Hey there, let's dig into this shared element transition issue you're facing—super frustrating when animations don't fire and views go missing, right? Even with matching transition names, there are a few common gotchas that might be tripping you up. Let's break down the possible fixes step by step:

1. Verify You're Starting the Activity Correctly

Shared element transitions require a specific startActivity call that passes the transition options. You can't use the plain startActivity(intent) method. Here's the correct way:

// In your Login Activity
val intent = Intent(this, TargetActivity::class.java)
val sharedTextView = findViewById<TextView>(R.id.your_login_textview_id)
val transitionOptions = ActivityOptions.makeSceneTransitionAnimation(
    this,
    sharedTextView,
    "your_matching_transition_name" // Must match the transition name in both layouts
)
startActivity(intent, transitionOptions.toBundle())

For Java, the logic is nearly identical—just ensure you're using ActivityOptions.makeSceneTransitionAnimation() and passing its bundle to startActivity().

2. Double-Check Transition Names (Case Sensitivity Matters!)

Make 100% sure the android:transitionName attribute is exactly the same (including capitalization) in both Activity layouts. A tiny typo here is one of the most common causes of broken transitions.

Example from Login Activity XML:

<TextView
    android:id="@+id/login_greeting"
    android:transitionName="greeting_text_transition"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Welcome Back!" />

Example from Target Activity XML:

<TextView
    android:id="@+id/home_greeting"
    android:transitionName="greeting_text_transition"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
3. Ensure Your Theme Enables Window Transitions

Your app's theme (or the target Activity's theme) must inherit from a Material Design-compatible theme and explicitly enable window content transitions. Add these lines to your styles.xml:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <!-- Enable transitions -->
    <item name="android:windowContentTransitions">true</item>
    <!-- Optional: Set default shared element animations -->
    <item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
    <item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>

If your target Activity uses a separate theme, confirm this setting is present there too.

4. Don't Modify the Shared View Too Early in the Target Activity

If you're updating the TextView's text, visibility, or layout properties immediately in onCreate() of the target Activity, you might interrupt the transition and cause the view to disappear. Wait until the transition completes before making changes:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_target)

    val sharedTextView = findViewById<TextView>(R.id.home_greeting)
    window.sharedElementEnterTransition.addListener(object : Transition.TransitionListener {
        override fun onTransitionStart(transition: Transition) {}
        override fun onTransitionEnd(transition: Transition) {
            // Safe to modify the view now
            sharedTextView.text = "Welcome to the Home Screen!"
        }
        override fun onTransitionCancel(transition: Transition) {}
        override fun onTransitionPause(transition: Transition) {}
        override fun onTransitionResume(transition: Transition) {}
    })
}
5. Confirm the Shared View is Visible and Loaded
  • The TextView in the Login Activity must be View.VISIBLE (not GONE or INVISIBLE) when you start the transition.
  • Avoid using ViewStub or dynamically added views as shared elements unless you're 100% sure they're fully inflated and visible before launching the target Activity.
6. Check Android Version Compatibility

Shared element transitions only work on Android 5.0 (API 21) and above. If you're testing on an older device, the animation won't run, and you might see unexpected view behavior. Add a version check to handle this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    // Run shared element transition logic
} else {
    // Fallback to regular startActivity
    startActivity(intent)
}

If you've gone through all these steps and still have issues, sharing the full XML of your target Activity and the complete startActivity logic from your Login Activity would help narrow things down further.

内容的提问来源于stack exchange,提问作者john

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:53:26