How do we know that the transition to a common activity item will be triggered?

To allow the transition of the common transition element to flow smoothly, I need to postpone heavy initialization in my target activity. See the following code:

getWindow().setSharedElementEnterTransition(enterTransition); enterTransition.addListener(new Transition.TransitionListener() { @Override public void onTransitionEnd(Transition transition) { init(); } }); 

However, if this action is launched from Deep link or another action that does not have a common element. A transition never starts, so onTransitionEnd() will never be called, and init() will never be triggered. In this case, I have to call init() right after the Activity starts.

How can I find out that the transition will be started?


EDIT I also want to start another input transition if a transition with a common element is not available. So the answer below is that using postponeEnterTransition() does not work for my case.

+5
source share
1 answer

It looks like you would be better off disabling postponeEnterTransition () in onCreate (or elsewhere) in your receive activity, inject all heavy init () after this call, and release the hold transition by explicitly calling startPostponedEnterTransition () after initialization is done. In the absence of a joint transition necessary to trigger activities such as DeepLink, etc., it will simply go straight to your heavy init without delay.

Here is the code:

Action A - initiates the transition of a common element.

 Intent ActivityDemoOneBIntent = new Intent(ActivityDemo1A.this, ActivityDemo1B.class); String transitionName = getString(R.string.activityTransitionName); Bundle optionsBundle = getTransitionOptionsBundle(imageViewAnimated, transitionName); startActivity(ActivityDemoOneBIntent, optionsBundle); 

Action B - β€œGets” the General Transition of an Element

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_demo_1_b); postponeTransition(); // postpone shared element transition until we release it explicitly // Do all heavy processing here, activity will not enter transition until you explicitly call startPostponedEnterTransition() // all heavy init() done startPostponedTransition() // release shared element transition. This can be placed to your listeners as well. } private void postponeTransition() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { postponeEnterTransition(); } else { ActivityCompat.postponeEnterTransition(this); } } private void startPostponedTransition() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { startPostponedEnterTransition(); } else { ActivityCompat.startPostponedEnterTransition(this); } } 
0
source

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


All Articles