ActivityOptions.makeSceneTransitionAnimation creates an empty activity

2.3k views Asked by At

I have two activities and a slide transition between them. Simple enough. But, for whatever reason ActivityOptions.makeSceneTransitionAnimation(Activity) seems to create an empty activity on the stack. Or rather, it keeps my original activity on the stack, but nulls out the root view so it looks like a blank activity.

I'm using the most vanilla way to create a scene transition from the docs. I'm not quite sure why this is a problem unless it's a known issue. Anyone else experience this? If so, have you fixed it?

public void startSearchActivity(MenuItem view) {
    Intent searchActivity = new Intent(this, SearchActivity.class);
    ActivityOptions transitionActivityOptions = ActivityOptions.makeSceneTransitionAnimation(HomeActivity.this);
    startActivity(searchActivity, transitionActivityOptions.toBundle());
}

private void setupWindowAnimations() {
    Transition slide = new Slide();
    slide.setDuration(1000);
    ((Slide)slide).setSlideEdge(Gravity.LEFT);
    getWindow().setEnterTransition(slide);
    getWindow().setReenterTransition(slide);
    getWindow().setExitTransition(slide);
}

And then in activity #2 I simply call either finish,onBackPressed, or finishFromTransition. None of which seem to get me back to the original activity I had on the stack. If I press back from activity #2, activity #1's onResume() does get called..but it's as if it never got its content set or any of its view initialized. If I launch the app again, the original activity has its onCreate get called which re-binds and relays out views.

Another odd thing to note is that the android soft "back", "home", and "recents" buttons aren't displayed when entering this blank activity. In fact, the only thing that looks like is retained from my app is the status bar color.

UPDATE: I've noticed that if I don't let the transition finish, and press back. It will go back to my previous activity just fine. It's only when the transition finishes and I make it to the next activity that I run into problems.

4

There are 4 answers

0
cj1098 On BEST ANSWER

Wow... I figured it out. Turns out it's out of our control for now. It's an issue with api 24+. I looked at makeSceneTransition and it has separate logic for those apis (of course). That's very disheartening. Hopefully google can fix this issue soon.

1
Sudhanshu Mittal On

Try this.

  1. Create a "anim" folder in your res/ directory.

  2. Inside the anim folder create 2 layout resource files named in_from_right.xml and out_from_left.xml

Add the following code to in_from_right.xml:

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

and the below code to out_from_left.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate android:fromXDelta="0%" android:toXDelta="-100%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="1000"/>
</set>

Now add this to your Activity.java file

try {
                Intent searchActivity = new Intent(this,SearchActivty.class);

                searchActivty.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(searchActivty);
            }catch (NullPointerException e){
                e.printStackTrace();
            }
            overridePendingTransition(R.anim.in_from_right, R.anim.out_from_left);

Just don't forget to add the anim folder.

It will work for you.

Happy coding.

0
LeonBre On

In my case I overwrote the wrong onCreate method for the new Activity. After inflating the layout on the other onCreate method everything worked fine :-)

2
BajaBob On

Figured it out. In my case I had some transitions defined in my styles.xml. Adding more animations to the activity programatically caused this behavior to occur (blank activity after pressing system back button). Just remove any traces of window*Transition from your styles.xml and you should be good to go.

    <item name="android:windowEnterTransition">@transition/slide_from_right</item>
    <item name="android:windowExitTransition">@transition/slide_from_right</item>