Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update source activity before exit transition

enter image description here

I need help about transition between activities:

I have two activities A and B and both have a ViewPager with the same image list. Every page has an ImageView with the transitionName equals to ​​​​​​​​image_x​​​​ where​​​​​​​​ x is the page index.

A starts activity B calling ActivityOptionsCompat.makeSceneTransitionAnimation and the enter transition is totally fine.

The problem is the following: when I close activity B, the exit transition does not initialize the view pager of activity A at the same position of B.

When user closes B, the latter sets the current page position in the result. In onActivityResult of activity A, I call the setCurrentItem and the behavior is showed in the gif.

Is there a way to update activity A before the exit transition starts?

like image 237
fran Avatar asked Dec 22 '15 11:12

fran


1 Answers

You should be able to do achieve that if you use setCurrentItem in the onActivityReenter instead of in onActivityResult (in your ActivityA).

Just be sure you:

  1. Before finishing ActivityB, set the result (either with setResult(int resultCode) or setResult(int resultCode, Intent data))
  2. Call supportFinishAfterTransition() (or finishAfterTransition()) instead of regular finish() to "close" the ActivityB.

To summerize:

in ActivityB:

public void close(){
    Intent data = new Intent();
    data.putExtra(KEY_CURRENT_ITEM, mFullscreenViewPager.getCurrentItem());
    setResult(RESULT_CODE, data);
    supportFinishAfterTransition();
}

in ActivityA:

@Override
public void onActivityReenter(int resultCode, Intent data) {
    super.onActivityReenter(resultCode, data);
    if (data.hasExtra(KEY_CURRENT_ITEM)){
         mViewPager.setCurrentItem(data.getIntExtra(KEY_CURRENT_ITEM, 0), false);
    }
}
like image 185
Bartek Lipinski Avatar answered Oct 03 '22 01:10

Bartek Lipinski