From dcf3bd0f7cde60252da156f04c0a52a44ce57731 Mon Sep 17 00:00:00 2001 From: Ben Weiss Date: Wed, 1 Jun 2016 15:32:09 +0100 Subject: [PATCH] Improve transition handling during sign in Change-Id: I60c521c89ad51b0dc80ddde507c74c35e40736a0 --- .../activity/CategorySelectionActivity.java | 3 +- .../apps/topeka/fragment/SignInFragment.java | 29 +++++++++--- .../widget/TransitionListenerAdapter.java | 44 +++++++++++++++++++ 3 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/com/google/samples/apps/topeka/widget/TransitionListenerAdapter.java diff --git a/app/src/main/java/com/google/samples/apps/topeka/activity/CategorySelectionActivity.java b/app/src/main/java/com/google/samples/apps/topeka/activity/CategorySelectionActivity.java index 5cf53c72..0dbec2ae 100644 --- a/app/src/main/java/com/google/samples/apps/topeka/activity/CategorySelectionActivity.java +++ b/app/src/main/java/com/google/samples/apps/topeka/activity/CategorySelectionActivity.java @@ -43,7 +43,6 @@ import com.google.samples.apps.topeka.helper.PreferencesHelper; import com.google.samples.apps.topeka.model.Player; import com.google.samples.apps.topeka.persistence.TopekaDatabaseHelper; -import com.google.samples.apps.topeka.widget.AvatarView; public class CategorySelectionActivity extends AppCompatActivity { @@ -138,7 +137,7 @@ private void signOut() { .inflateTransition(R.transition.category_enter)); } SignInActivity.start(this, false); - ActivityCompat.finishAfterTransition(this); + finish(); } private void attachCategoryGridFragment() { diff --git a/app/src/main/java/com/google/samples/apps/topeka/fragment/SignInFragment.java b/app/src/main/java/com/google/samples/apps/topeka/fragment/SignInFragment.java index 8e9959d0..375218d9 100644 --- a/app/src/main/java/com/google/samples/apps/topeka/fragment/SignInFragment.java +++ b/app/src/main/java/com/google/samples/apps/topeka/fragment/SignInFragment.java @@ -17,6 +17,7 @@ package com.google.samples.apps.topeka.fragment; import android.app.Activity; +import android.os.Build; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.FloatingActionButton; @@ -27,6 +28,7 @@ import android.support.v4.view.animation.FastOutSlowInInterpolator; import android.text.Editable; import android.text.TextWatcher; +import android.transition.Transition; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -37,10 +39,12 @@ import com.google.samples.apps.topeka.R; import com.google.samples.apps.topeka.activity.CategorySelectionActivity; import com.google.samples.apps.topeka.adapter.AvatarAdapter; +import com.google.samples.apps.topeka.helper.ApiLevelHelper; import com.google.samples.apps.topeka.helper.PreferencesHelper; import com.google.samples.apps.topeka.helper.TransitionHelper; import com.google.samples.apps.topeka.model.Avatar; import com.google.samples.apps.topeka.model.Player; +import com.google.samples.apps.topeka.widget.TransitionListenerAdapter; /** * Enable selection of an {@link Avatar} and user name. @@ -217,18 +221,29 @@ public void onItemClick(AdapterView parent, View view, int position, long id) private void performSignInWithTransition(View v) { final Activity activity = getActivity(); - if (v == null) { + if (v == null || ApiLevelHelper.isLowerThan(Build.VERSION_CODES.LOLLIPOP)) { // Don't run a transition if the passed view is null CategorySelectionActivity.start(activity, mPlayer); + activity.finish(); return; } - final Pair[] pairs = TransitionHelper.createSafeTransitionParticipants(activity, true, - new Pair<>(v, activity.getString(R.string.transition_avatar))); - @SuppressWarnings("unchecked") - ActivityOptionsCompat activityOptions = ActivityOptionsCompat - .makeSceneTransitionAnimation(activity, pairs); - CategorySelectionActivity.start(activity, mPlayer, activityOptions); + if (ApiLevelHelper.isAtLeast(Build.VERSION_CODES.LOLLIPOP)) { + activity.getWindow().getSharedElementExitTransition().addListener( + new TransitionListenerAdapter() { + @Override + public void onTransitionEnd(Transition transition) { + activity.finish(); + } + }); + + final Pair[] pairs = TransitionHelper.createSafeTransitionParticipants(activity, true, + new Pair<>(v, activity.getString(R.string.transition_avatar))); + @SuppressWarnings("unchecked") + ActivityOptionsCompat activityOptions = ActivityOptionsCompat + .makeSceneTransitionAnimation(activity, pairs); + CategorySelectionActivity.start(activity, mPlayer, activityOptions); + } } private void initContents() { diff --git a/app/src/main/java/com/google/samples/apps/topeka/widget/TransitionListenerAdapter.java b/app/src/main/java/com/google/samples/apps/topeka/widget/TransitionListenerAdapter.java new file mode 100644 index 00000000..bdfd8723 --- /dev/null +++ b/app/src/main/java/com/google/samples/apps/topeka/widget/TransitionListenerAdapter.java @@ -0,0 +1,44 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.samples.apps.topeka.widget; + +import android.annotation.TargetApi; +import android.os.Build; +import android.transition.Transition; + +/** + * Empty implementation of {@link Transition.TransitionListener}. + */ + +@TargetApi(Build.VERSION_CODES.KITKAT) +public abstract class TransitionListenerAdapter implements Transition.TransitionListener { + + @Override + public void onTransitionStart(Transition transition) { } + + @Override + public void onTransitionEnd(Transition transition) { } + + @Override + public void onTransitionCancel(Transition transition) { } + + @Override + public void onTransitionPause(Transition transition) { } + + @Override + public void onTransitionResume(Transition transition) { } +}