Skip to content Skip to sidebar Skip to footer

How Can One Know If An Activity Is Started Without A Transition?

I have a use case where I mostly start an activity with a transition, but that's not the case when opening it from the navigation drawer. To keep the transition smooth I have a Tra

Solution 1:

You can try onTransitionStart of TransitionListener to set some boolean isAnimationStarted.

publicclassSomeActivityextendsActivity {

    privateboolean isAnimationStarted = false;

    publicvoidonCreate(Bundle savedInstanceState) { 
        // ...if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            Transition sharedElementEnterTransition = getWindow().getSharedElementEnterTransition();
            sharedElementEnterTransition.addListener(newTransition.TransitionListener() {
                // ...@OverridepublicvoidonTransitionEnd(Transition transition) {
                    doSomeUiUpdating();
                }

                @OverridepublicvoidonTransitionStarted(Transition transition) {
                    isAnimationStarted = true;
                }
            });
        }
    }

    publicvoidonStart() {
        if (!isAnimationStarted) {
            doSomeUiUpdating();
        }
    }

}

Solution 2:

Since you are starting an Activity, you'll be making use of an Intent to start it. You can add extras to Intents and check for them in the onCreate() of the called Activity.

Let's assume that we have 2 Activities – ActivityA, and ActivityB.

Now, let's assume that ActivityA is the calling Activity, and that ActivityB is the called Activity.

In ActivityA, let's say you've written some code to start ActivityB with a SharedElementTransition.

@OverridepublicvoidonClick(View v) {
    IntentstartActivityBIntent=newIntent(getContext(), ActivityB.class);
    startActivityBIntent.putExtra("IS_SHARED_ELEMENT_TRANSITION_ENABLED", true);
    ActivityOptionsCompatactivityOptionsCompat= ActivityOptionsCompat.makeSceneTransitionAnimation(getActivity(), view, ViewCompat.getTransitionName(view));
    startActivity(startActivityBIntent, activityOptionsCompat);
}

Now, if you notice above, I've passed an Intent extra with the putExtra() method. I've passed a Boolean value of true because I intend to start the Activity with a SharedElementTransition.

Now in ActivityB's onCreate() method, you can just check for the boolean value passed to the Intent. If you passed false, then you can add a conditional statement and perform your UI updating there. I've given you a small snippet below to help you get started:

privatestaticfinalStringisSharedElementTransitionEnabled="IS_SHARED_ELEMENT_TRANSITION_ENABLED";    

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b);

    // If you are postponing your SharedElementTransition, don't forget to call postponeEnterTransition() and override onPreDraw()if (!getIntent().getExtras().getBoolean(isSharedElementTransitionEnabled)) {
        //Do your UI updation here
    }
}

The good thing about doing it this way is that you can then have full control over how your content transitions and your shared element transitions will play out.

Post a Comment for "How Can One Know If An Activity Is Started Without A Transition?"