Search code examples
androidkotlinandroid-videoview

How to move to another activity after a video is finished?


I am trying to increase the delay time if the user went to home screen and return to the app when the video is done. I want the app to move to the next activity. How can i do that?

My current code:

class MainActivity : AppCompatActivity() {
var currentp=0
var tmi:Long=7000
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var inte= Intent(this,Main_menu::class.java)
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        va.setBackgroundColor(Color.TRANSPARENT);

        val videoview : VideoView = va

        val uri = Uri.parse("android.resource://" + packageName + "/" + R.raw.intro)
        videoview.setVideoURI(uri)
        va.setZOrderOnTop(true)
        videoview.start()

        Handler().postDelayed({

    startActivity(inte)},7000)
}
     override fun onPause() {
        super.onPause()
currentp=va.currentPosition

     }

     override fun onStop() {
        super.onStop()
    }

    override fun onStart() {
        super.onStart()

    }

    override fun onResume() {
        super.onResume()
if(currentp!=0 && currentp!=100){
    va.seekTo(currentp)
        va.start()}
    }

Solution

  • JAVA

    videoView.setOnCompletionListener(completionListener);
    OnCompletionListener completionListener = new OnCompletionListener(){
    
            public void onCompletion(MediaPlayer mp) {
                Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
                startActivity(intent); 
            }
        };
    

    I usally code in Java but I tried to convert it in Kotlin, correct me if there is something wrong.

    KOTLIN

    videoView.setOnCompletionListener{
    
                var intent = Intent(this, MainMenu::class.java)                
                startActivity(intent); 
            }
        };
    

    Now you should have everything for getting on the right path. Cheers!