Replace Reverse SharedElementTransition when onBackPressed

I have 4 ImageButtonplaced in simple Layout. When I press any of these buttons, a new one starts TabActivityand the corresponding one is selected Tab(0-3). Tabs have a style ImageViewcorresponding to each of the previous buttons.

CHANGE WITH SOME PROGRESS BELOW

TL DR: How to set another SharedElementTransition when returning to the calling activity, so that the "back" transition is not the opposite of the original one, and possibly animates different Views?

I also use SharedElementTransition to animate the ImageButton to the selected tab, which works when I enter a new one Activity, but if I navigate to a Fragmentnew one Activityand press the "Back" button from a Fragmentdifferent one than the one I came to, the inverse SharedElementTransition does not match the Tabone I'm currently in. and it just cancels the transition used to start a new action.

I see that this is the expected behavior, but how can I override it? I want the reverse transition to begin with the current one Tab, and not from the Tabone from which I came from the previous one Activity.

I hope that this shameful picture helps to understand the question (it shows the behavior that I want to get), I start a new activity with the orange button, going to the fragment of the green tab and returning to the first action, moving from the green tab -): Jumpback problem

There is not much code in the question, but just in case, I run a new one Activitylike:

(...)
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(fromActivity, sharedView, transitionName);
fromActivity.startActivityForResult(toIntent, requestCode, options.toBundle());

And then, in the upcoming Activity, I set TabLayoutas follows in the method onCreate()(a similar 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 AVAILABLE

, - , Tab . , Tab ImageView s, onCreate() Tab. onBackPressed() Tab ImageView s Tab. , Tab, ImageView .

:

  • ImageView, , onActivityResult(...). , , , , .
  • (, , ) , , super.onBackPressed() Activity. .

, , Tab:

//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();
}

, 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);
}

, - ... .

+4

Source: https://habr.com/ru/post/1660320/


All Articles