Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one know if an activity is started without a transition?

Tags:

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 Transition.TransitionListener in which I trigger some UI updating when the transition is done.

So I have something like this:

public class SomeActivity extends Activity {

    public void onCreate(Bundle savedInstanceState){ 
        // ...
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            Transition sharedElementEnterTransition = getWindow().getSharedElementEnterTransition();
            sharedElementEnterTransition.addListener(new Transition.TransitionListener() {
                // ...
                @Override
                public void onTransitionEnd(Transition transition) {
                    doSomeUiUpdating();
                }
            });
        } else { // Pre-Lollipop
            doSomeUiUpdating();
        }
    }
}

This works well when starting the Activity with the animation, but how can I know if the Activity was started without a transition so that I can call doSomeUiUpdating()?

I'm sure there must be a simple method in Activity, Window, Transition or somewhere that I have overlooked. I don't want to relay on the calling Activity to set some bundle that telling if the animation is showing or not.

like image 241
Roy Solberg Avatar asked Feb 22 '16 20:02

Roy Solberg


People also ask

What is transition animation in android?

Android's transition framework allows you to animate all kinds of motion in your UI by simply providing the starting layout and the ending layout.

Why do we need to call setContentView () in onCreate () of activity class?

As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.

What is an activity in Android Studio?

An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.


2 Answers

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

public class SomeActivity extends Activity {

    private boolean isAnimationStarted = false;

    public void onCreate(Bundle savedInstanceState) { 
        // ...
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            Transition sharedElementEnterTransition = getWindow().getSharedElementEnterTransition();
            sharedElementEnterTransition.addListener(new Transition.TransitionListener() {
                // ...
                @Override
                public void onTransitionEnd(Transition transition) {
                    doSomeUiUpdating();
                }

                @Override
                public void onTransitionStarted(Transition transition) {
                    isAnimationStarted = true;
                }
            });
        }
    }

    public void onStart() {
        if (!isAnimationStarted) {
            doSomeUiUpdating();
        }
    }

}
like image 63
Pr38y Avatar answered Nov 01 '22 23:11

Pr38y


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.

@Override
public void onClick(View v) {
    Intent startActivityBIntent = new Intent(getContext(), ActivityB.class);
    startActivityBIntent.putExtra("IS_SHARED_ELEMENT_TRANSITION_ENABLED", true);
    ActivityOptionsCompat activityOptionsCompat = 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:

private static final String isSharedElementTransitionEnabled = "IS_SHARED_ELEMENT_TRANSITION_ENABLED";    

@Override
protected void onCreate(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.

like image 24
gadgetroid Avatar answered Nov 02 '22 01:11

gadgetroid