'How to move from Secondactivity to Mainactivity after animation is complete?

Sorry I am a bit new to stack overflow but hopefully the question is understandable!

I talked to a staff memeber/community member who showed me Intent to go from 1 activity(in my case GlobeActivity where my animation is stored) to another activity( MainActivity where username/password is stored). But the animation does not show up, instead it cuts directly to MainActivity.

Anyone got some suggestions to why and how to make it so that when animation is finished, it re-directs/ transitions to MainActivity without use of buttons?

Here is my code so far:

GlobeActivity( my SecondActivity):

package com.example.testerino2022

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class GlobeActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_globe)

        supportActionBar?.hide()

        val textView = findViewById<TextView>(R.id.textGlobeScreen)
        textView.animate().translationX(1050F).setDuration(1000).setStartDelay(2500)

        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

}

MainActivity(has default code in it );

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

    }
}


Solution 1:[1]

textView.animate()
   .translationX(1050F)
   .setDuration(1000)
   .setStartDelay(2500)
   .withEndAction {
       startActivity(Intent(context, MainActivity::class.java))
       finish()
  }

Solution 2:[2]

You can use animation listener

 textView.animate().translationX(1050F).setDuration(1000).setStartDelay(2500).setListener(
        object: Animator.AnimatorListener{
            override fun onAnimationStart(p0: Animator?) {
                TODO("Not yet implemented")
            }

            override fun onAnimationEnd(p0: Animator?) {
                val intent = Intent(this, MainActivity::class.java)
                startActivity(intent)
                finish()
            }

            override fun onAnimationCancel(p0: Animator?) {
                TODO("Not yet implemented")
            }

            override fun onAnimationRepeat(p0: Animator?) {
                TODO("Not yet implemented")
            }

        }
    )

Solution 3:[3]

You can do this easily with the ViewPropertyAnimator stuff you're already using - just add withEndAction:

textView.animate()
    .translationX(1050F)
    .setDuration(1000)
    .setStartDelay(2500)
    .withEndAction {
        startActivity(Intent(context, MainActivity::class.java))
        finish()
    }

Solution 4:[4]

Have you changed the launcher activity from MainActivity to GlobeActivity in the app's manifest?

Solution 5:[5]

You should listen to the view or element that is being animated, assuming an ImageView. You can do this with the ViewPropertyAnimator.

ImageView imageview = findViewById(R.id.imageView);

imageView.animate()
    .translationX(1050F)
    .setDuration(2000)
    .setStartDelay(1000)
    .withEndAction {
        startActivity(Intent(context, MainActivity::class.java))
        finish()
    }

Explanation

  1. translationX(float value) - This method will cause the View's translationX property to be animated to the specified value

  2. setDuration(long duration) - Sets the duration for the underlying animator that animates the requested properties.

  3. setStartDelay(long startDelay) - Sets the startDelay for the underlying animator that animates the requested properties.

  4. withEndAction(Runnable runnable) - Specifies an action to take place when the next animation ends.

For more information on this, check this out : -> ViewPropertyAnimator

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Yunus Dilber
Solution 3 cactustictacs
Solution 4 Abhinav Dwivedi
Solution 5 David Kariuki