Skip to content

Instantly share code, notes, and snippets.

@apherio
Last active April 29, 2016 21:12
Show Gist options
  • Save apherio/ab565c6210dac93fe03ac62748a83d19 to your computer and use it in GitHub Desktop.
Save apherio/ab565c6210dac93fe03ac62748a83d19 to your computer and use it in GitHub Desktop.
How to know when enter transition / onEnterAnimationComplete() ended on Fragment?
***Tweaking the animation timing****
From Lollipop, we can use onEnterAnimationComplete to check animation.So, in onCreate, if the SDK version is older than Lollipop, the RecyclerView can be populated.In Lollipop and newer, onEnterAnimationComplete will be called. Time to populate the RecyclerView and request a new layout animation is done like this
@Override public void onEnterAnimationComplete() {
super.onEnterAnimationComplete();
setRecyclerAdapter(recyclerView);
recyclerView.scheduleLayoutAnimation();
}
But Suppose we have a fragment, which shows enter animation, I set transition but also after that I want to show another animation.But we need to know when transition animation ends to start the second one.
For activity there is a callback like onEnterAnimationComplete() but it is not called when Fragment's transition ends.
Is there any better way to do know this ? I have an ugly work around for this problem .
The solution i thought of is using local broadcast manager
An activity that watches for notifications for the event named "custom-event-name".
IN your reciever activity
// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "custom-event-name".
@Override
public void onCreate(Bundle savedInstanceState) {
...
// Register to receive messages.
// We are registering an observer (mMessageReceiver) to receive Intents
// with actions named "custom-event-name".
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
new IntentFilter("custom-event-name"));
}
// Our handler for received Intents. This will be called whenever an Intent
// with an action named "custom-event-name" is broadcasted.
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("message");
Log.d("receiver", "Got message: " + message);
}
};
@Override
protected void onDestroy() {
// Unregister since the activity is about to be closed.
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
Now in your second activity that is the one which sends/broadcasts notifications.
@Override
public void onCreate(Bundle savedInstanceState) {
...
// Every time a button is clicked, we want to broadcast a notification.
findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
}
});
}
// Send an Intent with an action named "custom-event-name". The Intent sent should
// be received by the ReceiverActivity.
private void sendMessage() {
Log.d("sender", "Broadcasting message");
Intent intent = new Intent("custom-event-name");
// You can also include some extra data.
intent.putExtra("message", "This is my message!");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
Now we implement the onEnterAnimationComplete call in our Parent activity of our fragment .Time to populate the RecyclerView and request a new layout animation.
@Override public void onEnterAnimationComplete() {
super.onEnterAnimationComplete();
sendMessage();
}
Now similarly implementing a basic flag which will turn on and off when ever message is been passed we will come to know the transition animation has ended up.This may be the dirty lazy implementation but this is what made my animation go more smooth .For source implementation check my Philims project repo.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment