-2

I'm a beginner to Kotlin language. I have trouble redirection to another activity after for example 4 seconds. I know how to do it in Java but I have no idea about Kotlin. I use android studio version 2019.

I tried android studio's converter (Java to Kotlin) I couldn't find an answer which lead me to an answer that could redirect me from an activity to another by DELAY. I'm not asking about other ways of redirecting I'm asking exactly about delay. I mean how to go to another page in an android program not clicking any button or something.

edit (Sep 13): unfortunately I'm still struggling with the problem. I tried to use this code but It doesn't work. Can you please tell me what is the problem: MainActivity.kt:

package com.rearaa.weading
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
}
val handler = Handler()
val runnable = Runnable {
    val i = Intent(this@MainActivity, MenuPage::class.java)
    startActivity(i)
}
handler.postDelayed(runnable, 4000)

}

In above code I want to redirect from MainActivity to MenuPage.

shirazy
  • 109
  • 3
  • 11

7 Answers7

3

This how you can do it in android using Kotlin

private lateinit var mHandler: Handler
private lateinit var mRunnable: Runnable

Function :

private fun startMainActivity() {


    mRunnable = Runnable {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
    }

    mHandler = Handler()

    mHandler.postDelayed(mRunnable, 4000)
}

Remove Callbacks:

override fun onStop() {
    super.onStop()
    mHandler.removeCallbacks(mRunnable)
}
Jaymin
  • 2,879
  • 3
  • 19
  • 35
2

Use a Handler().postDelayed().
For example:

Handler().postDelayed({
      startActivity(Intent(this, SecondActivity::class.java))
    }, 4000); //4 Seconds
Darshan
  • 4,020
  • 2
  • 18
  • 49
1

Try this.

   val r = Runnable {
         //start your activity here
    }
    Handler().postDelayed(r, 3000)

3000 is delay time , you can change it to any value that you want.

Furqan Khan
  • 528
  • 4
  • 14
1

There are so many ways one of them is

 private lateinit var handler: Handler

Here is the function

 override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        handler = Handler()

        handler.postDelayed(object : Runnable {
            override fun run() {


                startActivity(
                    Intent(
                        this@SplashActivity,
                          LoginActivity::class.java
                    )
                )

                finish()


            }
        }, timeDelay)


    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null)
    }
Sunny
  • 3,134
  • 1
  • 17
  • 31
1

You can use Schedule

inline fun Timer.schedule(
    delay: Long, 
    crossinline action: TimerTask.() -> Unit
): TimerTask (source)

found it here: http://jamie.mccrindle.org/2013/02/exploring-kotlin-standard-library-part-3.html) import java.util.Timer import kotlin.concurrent.schedule

Timer("SettingUp", false).schedule(500) { 
   doSomething()
}
Kanwarpreet Singh
  • 2,037
  • 1
  • 13
  • 28
1

shortest sample:

        Handler().postDelayed({ startActivity(yourIntent()) }, 4000L)
1

Redirect to another activity(FirstActivity) from MainActivity after 4 second:

val handler = Handler()
        val runnable = Runnable {
            val i = Intent(this@MainActivity, FirstActivity::class.java)
            startActivity(i)
        }
        handler.postDelayed(runnable, 4000)
Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36