Android Tutorial Custom transitions between activities

This short tutorial will explain how to create custom transitions between activities.

The transitions must be declared in an XML file as described here: Animation Resources | Android Developers

1. Declare the transition

You should put the animation files under Objects\Res\anim folder.
The files must be set to be read-only. Otherwise they will be deleted.

After changing the files you should click on Tools - Clean Project. This will cause the IDE to recreate the resources mapping file (R.java).

2. Call SetAnimation

You should add this code to your project (requires Reflection library):
B4X:
Sub SetAnimation(InAnimation As String, OutAnimation As String)
    Dim r As Reflector
    Dim package As String
    Dim in, out As Int
   package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
    in = r.GetStaticField(package & ".R$anim", InAnimation)
    out = r.GetStaticField(package & ".R$anim", OutAnimation)
    r.Target = r.GetActivity
    r.RunMethod4("overridePendingTransition", Array As Object(in, out), Array As String("java.lang.int", "java.lang.int"))
End Sub

After calling StartActivity or Activity.Finish you should call SetAnimation.
For example:
B4X:
StartActivity(Activity2)
SetAnimation("zoom_enter", "zoom_exit") 'without the xml extension

The attached example demonstrates the transition. You should press on Connect and then on one of the images.
Pressing on the back key when the second activity is visible will also start a transition.
The animations are based on this example: Animation.java | Android Developers
 

Attachments

  • FlickrViewer_WithActivityAnimation.zip
    11.1 KB · Views: 3,684

awoo060

Member
Licensed User
Longtime User
Hey Erel, I've created a simple transition that has a duration of zero seconds which works fine the first time I start an activity using StartActivity(). But when I resume an activity that has been kept open, it reverts to the default transition. I've tried moving the SetAnimation code into the Activity_Pause routine of the activity that is moving to the background with no luck.

I've also ensured there is no specific code called from Activity_Create that might be effecting the results, as this would not be called the second time during Activity_Resume (as I understand)

Any suggestions?
 

awoo060

Member
Licensed User
Longtime User
Thanks for the quick reply, hadn't noticed this had gone onto a new page... Apparently I can't upload as the file is too big? Its a pretty small project, but comes to ~550K when zipped...
 

awoo060

Member
Licensed User
Longtime User
Hey Erel, I've rezipped it and included some instructions as well. I removed the apk and classes.dex which seemed to be the biggest files in there and seem to be getting rebuilt and compile time. Please let me know if you have any issues getting it up and running or missing libraries.

Cheers,
Ant.
 

Attachments

  • Demo2.zip
    270.8 KB · Views: 626

awoo060

Member
Licensed User
Longtime User
Sorry mate,

So you'll start on tab 1 - the tabs are at the bottom. Move along to 2, 3, 4 and 5 and you'll see the intended effect. When doing this activity 2, 3, 4 and 5 are opened respectively and kept open. If you then go back in the opposite direction, you will see that the default fade in transition is being used instead of the custom one found under /objects/res/anim.

Thanks in advance for any advice.
 
Top