I have been working on an Android application where I need to make an animation for my activity. I used overridePendingTransition(entr,exit) and made 3 xml files in res\anim folder. which r push_up_in.xml, push_down_out.xml, hold.xml using
<set>
<translate>
duration, fromYDelta, toYDelta, zAdjustment
</set>
I kept zAdjustment=top for push_.xmls and bottom for hold.xml. everything works fine, when I startactivity(myIntent) and finish()
my activity the animation works flawless in both cases.
BUT when I change my activity theme from AndroidManifest.xml file to "Theme.Dialog" theoverridePendingAnimation on MyActivity.this.finish();
never works. It means my animation works when I start activity but not work when I finish an Activity.
AndroidManifest.xml
<activity
android:name=".MyActivity"
android:label="My Activity"
android:theme="@android:style/Theme.Dialog">
</activity>
If I simply remove the "Theme" statement, everything again start working like a charm.
<activity
android:name=".MyActivity"
android:label="My Activity">
</activity>
but I want my activity to look like a dialog.
I ran into the same problem in Android 4.4.2. Theme.Dialog defines a default very short exit animation that quickly downscales the dialog and fades it to transparent. Calling overridePendingTransition(entr,exit) on a dialog runs the exit animation you specify concurrently with the Theme.Dialog animation instead of replacing it. Furthermore, the animation you specify unexpectedly terminates when the Theme.Dialog animation completes even if yours is much longer. If you look very closely you may notice that your exit animation is actually starting to run but gets terminated after 50-100ms.
This appears to be an Android bug, but I found a workaround. You need to replace the Theme.Dialog exit animation with a dummy animation defined as a style in XML. For example, create a file named no_animation.xml containing the following in your res/anim folder:
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="1.0"
android:duration="500" />
Then add the following to your res/style file:
<style name="NoDialogAnimation">
<item name="android:windowEnterAnimation">@anim/no_animation</item>
<item name="android:windowExitAnimation">@anim/no_animation</item>
</style>
Now you can replace the default Theme.Dialog animation with NoDialogAnimation in either of the following ways:
Option 1. Create a new style named MyDialogStyle as follows:
<style name="MyDialogStyle" parent="@android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">@style/NoDialogAnimation</item>
</style>
Then add this line to your activity in the manifest file:
android:theme="@style/MyDialogStyle"
Option 2. Add the following line to your activity's onCreate():
getWindow().getAttributes().windowAnimations = R.style.NoDialogAnimation;
The exit animation you specify in overridePendingTransition(entr,exit) should now run for 500ms because that is the duration of NoDialogAnimation. You are probably wondering why not just specify your desired animation as a style instead of calling overridePendingTransition. Well, if you do that the default Theme.Dialog animation will magically run first, even though you replaced it, then your animation will pick up midway through and finish. Like I said before, this looks like an Android bug.
Final note:
I recommend specifying a noDialogAnimation duration that is about 50ms less than the duration you specify in your actual exit animation. The reason is that the animations don't appear to be synchronized. If your animation finishes first the dialog will bounce back to its original position for a few milliseconds just before it disappears. Subtracting 50ms seemed to avoid that problem.
This is an old question, but here's what worked for me. I faced this issue on a v4.2.2 device and it's caused by the default window animation style in Theme.Dialog, as Dan suggested. All you need to do is to simply get rid of it, by customizing your theme:
<style name="CustomTheme" parent="@android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">@null</item>
<item name="android:windowEnterAnimation">@null</item>
<item name="android:windowExitAnimation">@null</item>
<!-- other customizations -->
</style>
I've set android:windowEnterAnimation and android:windowExitAnimation to null as well, just in case. Now you have to use your custom theme in the manifest and you're done:
<activity
android:name=".MyActivity"
android:label="My Activity"
android:theme="@style/CustomTheme">
</activity>