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:
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().
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" />
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.
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) {} }) }
- The TextView in the Login Activity must be
View.VISIBLE(notGONEorINVISIBLE) when you start the transition. - Avoid using
ViewStubor dynamically added views as shared elements unless you're 100% sure they're fully inflated and visible before launching the target Activity.
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

