nullbyte
nullbyte

Reputation: 1216

How to handle return transition with shared elements?

I want to implement a custom Pre-Lollipop transition. I have a shared element in both activities(A and B), that is a simple ImageView. My custom transitions with shared elements work by the same principle as native transitions do. First of all, I set the background of B activity to transparent, then I capture the start state of the shared element and pass this data to B activity. In B I capture the end state of the shared view and create an Animator that will animate the view between the two states, of course that animation happens in B activity.

At this point, as long as I have the same shared element in both activities everything is perfect, and I'm able to apply appropriate return transition. However, when I want to implement some kind of gallery(when you can slide images to the left and right in B activity) I have a problem with return transition. In this case, both activities share the same source of data, so the user can simply change the shared element in B activity. So, what if I change that shared element in the second activity? Then, obviously, in order to implement return transition I need to know the size and position of that element in A activity, right?

Basically, we have only 2 scenarios (correct me, if I'm wrong) to do so:

Moreover, I also have to redraw a RecyclerView or whatever that keeps the data using requestLayout() or invalidate() method.

I know that I can implement all this stuff with native transitions using SharedElementCallback callback and onActivityReenter() method. This framework somehow handles to apply this return transition in the called activity not in the caller.

So, I'm wondering if there is any alternative?

Upvotes: 0

Views: 837

Answers (1)

nullbyte
nullbyte

Reputation: 1216

Finally, I've found an answer to my question. As I said later, we have only two scenarios, so I've implemented the second. We can apply publish–subscribe pattern in this case, thus it makes it possible to retrieve the data out of the first activity and send the requested data to the second activity. As long as I have the appropriate data in the second activity, I am able to animate the image back to its position in the first activity.

To implement publish-subscribe pattern, I used Otto, which is an event bus that provides a mechanism that you can use to communicate with different parts of your application.

You can find the implementation of this idea here. Also you can find there a sample of using native transitions.

Upvotes: 0

Related Questions