I have 4 ImageButton
s placed in a simple Layout
. When I click any of these buttons, a new TabActivity
is launched, and the corresponding Tab
is selected (0-3). The tabs are styled with an ImageView
matching one of the previous buttons each.
EDIT WITH SOME PROGRESS BELOW
TL;DR: How to set a different SharedElementTransition when returning to the calling Activity, so that the "back" transition is not a reverse of the original one, and possibly animate different View
s?
I also use a SharedElementTransition to animate the ImageButton into the selected tab, which works when entering the new Activity
, but if I navigate through the Fragment
s of the new Activity
, and press the back button from a Fragment
different from the one I arrived to, the reverse SharedElementTransition doesn't match the Tab
I'm currently on, and it just reverses the transition used to launch the new activity.
I can see this is the expected behavior, but how can I override it? I want the reverse transition to start from the current Tab
, not from the Tab
I arrived at from the previous Activity
.
I hope this ignominious picture helps understanding the question (it shows the behavior I want to obtain -starting new activity from the orange button, navigating to the green tab's fragment, and pressing back returning to the first activity transitioning from the green tab-):
There's no much code involved in the question, but just in case I am launching the new Activity
as:
(...)
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(fromActivity, sharedView, transitionName);
fromActivity.startActivityForResult(toIntent, requestCode, options.toBundle());
And then, in the arriving Activity
, I set up the TabLayout
as follows in the onCreate()
method (analogous piece of code for each Tab
:
tab0 = (...);
ImageView v0 = (ImageView) tab0.findViewById(R.id.tab_img);
ApiLevelHelper.setImageDrawable(this, v0, R.drawable.menu_btn_0);
tabLayout.getTabAt(0).setCustomView(tab0);
Thanks in advance
EDIT - SOME PROGRESS ACHIEVED
Well, There's still an important problem to solve, but I've managed to get the right Tab
transitioning on the reverse animation. In order to achieve this, I have no transition name previously attached to the Tab
's ImageView
s, and I set the transition name on the onCreate()
method of the new activity only to the selected Tab
. Then inside the onBackPressed()
call, I clear all transition names from all Tab
's ImageView
s, and set it to the current Tab
. This way the reverse animation starts from the correct Tab
, but it ends up in the original ImageView
's position, instead of the new one.
What I've tried so far:
- Clearing transition names on the first activity and setting it just to the ImageView I want to reverse-transition to, inside the
onActivityResult(...)
method. Sadly enough, the animation conditions seem to be stablished before this method is called. - Staticly referencing the parent activity (knowing this is bad, just testing) and clearing transition names and setting just the one I want before calling
super.onBackPressed()
from the secondActivity
. It didn't change anything.
Some code showing how the transition name of the current Tab
is set:
//Second Activity
@Override
public void onBackPressed() {
if (ApiLevelHelper.isAtLeast(Build.VERSION_CODES.LOLLIPOP)) {
for (ImageView v : tabImages) {
v.setTransitionName(null);
}
int selectedTab = mTabLayout.getSelectedTabPosition();
tabImages[selectedTab].setTransitionName(getString(R.string.transition_menu_tab));
Intent output = new Intent();
output.putExtra("selected_tab", selectedTab);
setResult(RESULT_OK, output);
}
super.onBackPressed();
}
And then, in the first Activity
...:
//First Activity
//This code has no effect right now on the ending position of the transitioned view.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQ_WHICH_TAB && data != null) {
if (ApiLevelHelper.isAtLeast(Build.VERSION_CODES.LOLLIPOP)) {
int selectedTab = data.getIntExtra("selected_tab", 0);
setTransitionName(selectedTab); //This call nullifies all transition names
//and sets it back to the right ImageView.
//But it doesn't work.
}
}
super.onActivityResult(requestCode, resultCode, data);
}
I think I somehow need to manipulate the previously calculated transform... still investigating.