I have a use case where I mostly start an activity with a transition, but that's not the case when opening it from the navigation drawer.
To keep the transition smooth I have a Transition.TransitionListener
in which I trigger some UI updating when the transition is done.
So I have something like this:
public class SomeActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
// ...
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Transition sharedElementEnterTransition = getWindow().getSharedElementEnterTransition();
sharedElementEnterTransition.addListener(new Transition.TransitionListener() {
// ...
@Override
public void onTransitionEnd(Transition transition) {
doSomeUiUpdating();
}
});
} else { // Pre-Lollipop
doSomeUiUpdating();
}
}
}
This works well when starting the Activity with the animation, but how can I know if the Activity was started without a transition so that I can call doSomeUiUpdating()
?
I'm sure there must be a simple method in Activity
, Window
, Transition
or somewhere that I have overlooked. I don't want to relay on the calling Activity to set some bundle that telling if the animation is showing or not.
Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.
As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.
An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.
You can try onTransitionStart
of TransitionListener
to set some boolean isAnimationStarted
.
public class SomeActivity extends Activity {
private boolean isAnimationStarted = false;
public void onCreate(Bundle savedInstanceState) {
// ...
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Transition sharedElementEnterTransition = getWindow().getSharedElementEnterTransition();
sharedElementEnterTransition.addListener(new Transition.TransitionListener() {
// ...
@Override
public void onTransitionEnd(Transition transition) {
doSomeUiUpdating();
}
@Override
public void onTransitionStarted(Transition transition) {
isAnimationStarted = true;
}
});
}
}
public void onStart() {
if (!isAnimationStarted) {
doSomeUiUpdating();
}
}
}
Since you are starting an Activity
, you'll be making use of an Intent
to start it. You can add extras to Intent
s and check for them in the onCreate()
of the called Activity
.
Let's assume that we have 2 Activities – ActivityA
, and ActivityB
.
Now, let's assume that ActivityA
is the calling Activity
, and that ActivityB
is the called Activity
.
In ActivityA
, let's say you've written some code to start ActivityB
with a SharedElementTransition
.
@Override
public void onClick(View v) {
Intent startActivityBIntent = new Intent(getContext(), ActivityB.class);
startActivityBIntent.putExtra("IS_SHARED_ELEMENT_TRANSITION_ENABLED", true);
ActivityOptionsCompat activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), view, ViewCompat.getTransitionName(view));
startActivity(startActivityBIntent, activityOptionsCompat);
}
Now, if you notice above, I've passed an Intent
extra with the putExtra()
method. I've passed a Boolean
value of true because I intend to start the Activity with a SharedElementTransition
.
Now in ActivityB
's onCreate()
method, you can just check for the boolean value passed to the Intent
. If you passed false, then you can add a conditional statement and perform your UI updating there. I've given you a small snippet below to help you get started:
private static final String isSharedElementTransitionEnabled = "IS_SHARED_ELEMENT_TRANSITION_ENABLED";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
// If you are postponing your SharedElementTransition, don't forget to call postponeEnterTransition() and override onPreDraw()
if (!getIntent().getExtras().getBoolean(isSharedElementTransitionEnabled)) {
//Do your UI updation here
}
}
The good thing about doing it this way is that you can then have full control over how your content transitions and your shared element transitions will play out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With