Reputation: 2243
I have sequence animation. Animation direction is 420mm.When animation finished I try to start another activity with ActivityOptionsCompat (bottom to top animation).Here is a my code
private void startSceneAnimation() {
if (imageView != null) {
((AnimationDrawable) imageView.getBackground()).start();
new Handler().postDelayed(() -> {
Intent intent = new Intent(LoginTestActivity.this,
SPLoginActivity.class);
ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(LoginTestActivity.this,
logo,
ViewCompat.getTransitionName(logo));
startActivity(intent, options.toBundle());
}, 420);
}
}
Everything working perfect ,but I have one question.Is a any way to starting new activity when animation still finished(without stop animation).I try to start new activity after 300 mm but my sequence animation finished
Upvotes: 3
Views: 231
Reputation: 569
The AnimationDrawable
is executed on the UI Thread. An anonymous Handler
instance generated with no parameters passed in for the Looper
also runs on the UI Thread. Hence, queuing the activity to change on the Handler
after any amount of time may not occur until after the AnimationDrawable
has executed finish, because they are both on the same Thread
Try using a Timer
instead?
Timer timer = new Timer();
timer.schedule(new TimerTask(){
public void run() {
Intent intent = new Intent(LoginTestActivity.this,
SPLoginActivity.class);
ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(LoginTestActivity.this,
logo,
ViewCompat.getTransitionName(logo));
startActivity(intent, options.toBundle());
}
}, 300);
Timer
runs in a background thread, and since it exits quickly it shouldn't cause any memory leaks AFAIK.
EDIT: It appears that starting an Activity is done internally (line 4614) in the Android framework and can be wonky if attempted from outside the UI thread. You could try to post the new Intent at the front of the UI Thread Message Queue and see if that works, but it will probably cause the animation to "stop" anyway because that same UI Thread is now being used to handle the activity launch.
Timer timer = new Timer();
timer.schedule(new TimerTask(){
public void run() {
new Handler(Looper.getMainLooper()).postAtFrontOfQueue(() -> {
Intent intent = new Intent(LoginTestActivity.this,
SPLoginActivity.class);
ActivityOptionsCompat options = ActivityOptionsCompat.
makeSceneTransitionAnimation(LoginTestActivity.this,
logo,
ViewCompat.getTransitionName(logo));
startActivity(intent, options.toBundle());
});
}
}, 300);
Upvotes: 1