In order to let shared element transition run smoothly, I need to postpone the heavy initialization at my destination activity. See code below:
getWindow().setSharedElementEnterTransition(enterTransition);
enterTransition.addListener(new Transition.TransitionListener() {
@Override
public void onTransitionEnd(Transition transition) {
init();
}
});
However, if this Activity is started from Deep link
or another activity that do not have shared element. The transition never start, thus onTransitionEnd()
never be called and init()
will never run. In that case, I should call init()
immediately after the Activity started.
How can I know that the transition is going to run?
EDIT
I also want to run another enter transition if the shared element transition is not available. So answer below that suggest using postponeEnterTransition()
does not work for my case.
It looks like you'd be better off calling postponeEnterTransition() in onCreate (or anywhere else) in your receiving activity, implementing all heavy init() after that call, and releasing transition hold by explicitly calling startPostponedEnterTransition() once init is done. In case when there is no shared transition required to start the activity, such as DeepLink and etc., this will just go straight to your heavy init with no delay.
Here is the code:
Activity A - starts shared element transition
Intent ActivityDemoOneBIntent = new Intent(ActivityDemo1A.this, ActivityDemo1B.class);
String transitionName = getString(R.string.activityTransitionName);
Bundle optionsBundle = getTransitionOptionsBundle(imageViewAnimated, transitionName);
startActivity(ActivityDemoOneBIntent, optionsBundle);
Activity B - "receives" shared element transition
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_1_b);
postponeTransition(); // postpone shared element transition until we release it explicitly
// Do all heavy processing here, activity will not enter transition until you explicitly call startPostponedEnterTransition()
// all heavy init() done
startPostponedTransition() // release shared element transition. This can be placed to your listeners as well.
}
private void postponeTransition() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
postponeEnterTransition();
} else {
ActivityCompat.postponeEnterTransition(this);
}
}
private void startPostponedTransition() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
startPostponedEnterTransition();
} else {
ActivityCompat.startPostponedEnterTransition(this);
}
}
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