Reputation: 68
Apparently I'm doing all the steps correctly, but the transition isn't working properly.
I opened a new very simple project, with only two activities, in the MainActivity layout I have an ImageView centered and in SecondActivity I have the same ImageView, but bigger
The following happens:
When I open the application and click on the image, it opens the SecondActivity but no transition, now still in SecondActivity, when I press the back button, it goes back to MainActivity, but still without transition. And from now on, when I click on ImageView, SecondActivity doesn't open anymore, however, when I minimize the application (when going through onPause) and come back, SecondActivity is open, and when I press the back button, the "reverse transition " occurs correctly.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/menu_image_view"
android:layout_width="200dp"
android:layout_height="300dp"
android:clickable="true"
android:focusable="true"
android:scaleType="centerCrop"
android:src="@drawable/cardapio"
android:transitionName="image"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val menuImageView = findViewById<ImageView>(R.id.menu_image_view)
menuImageView.transitionName = "image"
val i = Intent(this, SecondActivity::class.java)
val options: Bundle? = ActivityOptionsCompat.makeSceneTransitionAnimation(
this, menuImageView, "image"
).toBundle()
menuImageView.setOnClickListener {
startActivity(i, options)
}
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<ImageView
android:layout_width="400dp"
android:layout_height="600dp"
android:scaleType="centerCrop"
android:src="@drawable/cardapio"
android:transitionName="image"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
SecondActivity.kt
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
}
override fun onBackPressed() {
// To support reverse transitions when user clicks the device back button
supportFinishAfterTransition()
}
}
And my themes.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Animations" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
<item name="android:windowActivityTransitions">true</item>
</style>
</resources>
I've already tried using both the
<item name="android:windowActivityTransitions">true</item>
and the
<item name="android:windowContentTransitions">true</item>
Could it be an Android Studio bug?
Upvotes: 1
Views: 68
Reputation: 2377
You have to them inside onClickListener
. If you put inside onCreate
, it will create it once the activity is created. When you minimized the apps, only onPaused
will be triggered.
menuImageView.setOnClickListener {
val i = Intent(this, SecondActivity::class.java)
val options: Bundle? = ActivityOptionsCompat.makeSceneTransitionAnimation(
this, menuImageView, "image"
).toBundle()
startActivity(i, options)
}
Upvotes: 1