Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity transition black screen

So I've got WelcomeActivity -> HomeActivity and closing WelcomeActivity with finish()/supportFinishAfterTransition(). I want to do either a slideTransition or a fadeTransition (open to other suggestions btw).

I've researched this and as it turns out there are 2+ ways of doing it: either with overridePendingTransition which uses anim.xml files or with Transitions (from the android docs) which use transition.xml files...

I've tried both and both give me unwanted results:

  1. for anims: I get this ugly mid transition black screen:

fade_in.xml:

    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromAlpha="0.0"
       android:toAlpha="1.0"
       android:duration="300" />

fade_out.xml:

    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
       android:interpolator="@android:anim/accelerate_interpolator"
       android:fromAlpha="1.0"
       android:toAlpha="0.0"
       android:zAdjustment="top"
       android:duration="300" />

WelcomeActivity: (I've tried having finish before the overridePendingTransaction)

    startActivity(Intent(this, HomeActivity::class.java))
    overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
    finish()
  1. for transitions: I can't make it so WelcomeActivity closes properly: It either closes before the animation starts or not closing at all. I'm following the android docs.. I've also tried this:

style.xml

    <item name="android:windowActivityTransitions">true</item>
    <item name="android:windowEnterTransition">@transition/enter_fade</item>
    <item name="android:windowExitTransition">@transition/exit_fade</item>

My other questions is which approach should I have? Is Google pushing the transitions over the anims for starting new activities?

like image 355
Martin Avatar asked Nov 07 '22 20:11

Martin


1 Answers

What I always do is to start an activity(any way you want, ways are listed here).
I use slide transitions using these two files:

slide_out_left.xml:

<?xml version="1.0" encoding="utf-8"?>
 
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />
</set>

slide_in_right.xml:

<?xml version="1.0" encoding="utf-8"?>
 
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromXDelta="100%p"
        android:toXDelta="0" />
</set>

Then I start an activity like this(this is java):

startActivity(MainActivity.this, SecondActivity.class);
overridePendingTransition(R.anim.slide_in_right.xml, R.anim.slide_in_left.xml);
finish();

Using this, the activity exits giving way to the new one smoothly from right to left.
For the black screen, set the theme of that activity as translucent in the AndroidManifest.xml file

android:theme="@android:style/Theme.Translucent"

so your code will be something like this

<activity android:name=".Activity"
        android:theme="@android:style/Theme.Translucent" />

Answer for the black screen taken from: https://stackoverflow.com/a/6468734/9819031

like image 152
Gourav Avatar answered Nov 15 '22 08:11

Gourav