1

I have created a transition manager in resources and run a transition when one of the buttons on my main layout is clicked. The idea is very simple: run transition inflated from resources and transition to scene_login when the login button is clicked, and run the same transition when the signup button is clicked and transition to scene_signup. When the back button is clicked, undo the transitions smoothly, and be ready for click events on the buttons again.
A Boolean variable is used to keep track of whether the a transition is started. After a transition run, the buttons do not receive the onClick events anymore. This means that I cannot run subsequent transitions, which will run inside onClick(). Never encountered this before. Any idea?

R.transition.transition_mgr:

<?xml version="1.0" encoding="utf-8"?>
<transitionManager xmlns:android="http://schemas.android.com/apk/res/android">
  <transition android:transition="@transition/mytransitions"  android:toScene="@layout/scene_signup"/>
  <transition android:transition="@transition/mytransitions" android:toScene="@layout/scene_login"/>
  <transition android:transition="@transition/mytransitions" android:toScene="@layout/activity_main"/>
</transitionManager>

R.transition.mytransitions:

<?xml version="1.0" encoding="utf-8"?>

<transitionSet android:duration="3000"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <changeBounds/>
    <fade/>
</transitionSet>

R.layout.activity_main:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/signup"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Sign Up"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/login"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Log In"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="0dp"
        app:layout_constraintGuide_percent=".5" />

</android.support.constraint.ConstraintLayout>

R.layout.scene_login:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/email"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:ems="10"
        android:hint="Email"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <EditText
        android:id="@+id/password"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="20dp"
        app:layout_constraintGuide_percent=".5" />

    <Button
        android:id="@+id/login"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:text="Log In"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/password" />

    <Button
        android:id="@+id/signup"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Sign Up"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/phone" />

</android.support.constraint.ConstraintLayout>

R.layout.scene_signup:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/login"
        android:layout_width="200dp"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:text="Log In"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/phone" />

    <Button
        android:id="@+id/signup"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Sign Up"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/phone" />

    <EditText
        android:id="@+id/password"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/email" />

    <EditText
        android:id="@+id/confirmPassword"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Password (again)"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/password" />

    <EditText
        android:id="@+id/email"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="100dp"
        android:ems="10"
        android:hint="Email"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/phone"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="Phone"
        android:inputType="phone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/confirmPassword" />

</android.support.constraint.ConstraintLayout>

MainActivity.java:

public class MainActivity extends AppCompatActivity{

    private static final String TAG = "MainActivity";
    Button signupButton;
    Button loginButton;
    boolean transitionStarted = false;
    TransitionManager transitionMgr;
    Scene scene_main;
    Scene scene_login;
    Scene scene_signup;
    ViewGroup root;
    Context mContext = this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        signupButton = findViewById(R.id.signup);
        loginButton = findViewById(R.id.login);


        root = findViewById(R.id.root);
        transitionMgr = TransitionInflater.from(mContext).inflateTransitionManager(R.transition.transition_mgr, root);
        scene_main = Scene.getSceneForLayout(root, R.layout.activity_main, mContext);
        scene_login = Scene.getSceneForLayout(root, R.layout.scene_login, mContext);
        scene_signup = Scene.getSceneForLayout(root, R.layout.scene_signup, mContext);

        signupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "onClick: transitionStarted: " + transitionStarted );

                if(!transitionStarted) {
                    transitionMgr.transitionTo(scene_signup);
                    transitionStarted = true;
                }
            }
        });
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "onClick: transitionStarted: " + transitionStarted);

                if(!transitionStarted) {
                    transitionMgr.transitionTo(scene_login);
                    transitionStarted = true;
                }
            }
        });





    }

    @Override
    public void onBackPressed() {
        Log.d(TAG, "onBackPressed: transitionStarted: " + transitionStarted);
        if(transitionStarted) {
            transitionStarted = false;
            transitionMgr.transitionTo(scene_main);
        }
        else
        super.onBackPressed();
    }

}

PS. It seems that by transitioning to a new scene, information of views including any onClick listeners is cleared, and we probably need to find the views again and set required listeners.

7
  • that is a whole bunch of code. Commented Jul 12, 2018 at 17:41
  • Sorry @DroiDev let me add some explanation. Commented Jul 12, 2018 at 17:42
  • what is the transition you are doing? fading? Commented Jul 12, 2018 at 17:46
  • @DroiDev fade and changeBounds. The problem is that after the first transition, the click events are not triggered anymore. Commented Jul 12, 2018 at 17:48
  • main activity to login.... then when u go back to the main activity, the onclick doesnt work again? im never done this, but the logic tells me that you need to reset the on click to true on onResume Commented Jul 12, 2018 at 17:51

1 Answer 1

0

Note that after transitioning to a new scene, view info like onClick listeners are cleared since view hierarchy changes, despite the fact that you might create views in different with the same IDs.
To keep onClick working after transition, you need to find respective views again and reset onClick listeners on them.

Revised activity code (working) (Also on GitHub):

class MainActivity : AppCompatActivity(), View.OnClickListener {
    override fun onClick(p0: View?) {
        when(p0?.id) {
            R.id.register -> {
                if (!animationStarted) {
                    TransitionManager.go(sceneRegister, set);
                    animationStarted = true;
                }
            }
            R.id.login -> {
                if(!animationStarted) {
                    TransitionManager.go(sceneLogin, set);
                    animationStarted = true;
                }
            }

        }

    }


    var animationStarted = false;

    lateinit var scene: Scene
    lateinit var login: Button
    lateinit var register: Button
    lateinit var root : ViewGroup
    lateinit var sceneRegister: Scene
    lateinit var sceneLogin: Scene
    lateinit var set: TransitionSet


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
        login = findViewById<Button>(R.id.login);
        register = findViewById<Button>(R.id.register);
        root = findViewById<ViewGroup>(R.id.root);
        scene = Scene.getSceneForLayout(root, R.layout.main_activity, this);
        sceneLogin = Scene.getSceneForLayout(root, R.layout.main_activity_login, this);
        sceneRegister = Scene.getSceneForLayout(root, R.layout.main_activity_register, this);

        set = TransitionSet();

        set.addTransition(ChangeBounds()).addTransition(Fade()).addTransition(AutoTransition());

        login.setOnClickListener (this)
        register.setOnClickListener(this)




    }

    override fun onBackPressed() {
        if(animationStarted) {
            val root = findViewById<ViewGroup>(R.id.root);
            TransitionManager.go(scene, ChangeBounds() );
            animationStarted = false;
            login = findViewById(R.id.login)
            register = findViewById(R.id.register)

            login.setOnClickListener(this)
            register.setOnClickListener(this)
        }
        else {

            super.onBackPressed();
        }

    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.