Prabhakaran P
Prabhakaran P

Reputation: 281

How to Convert java to kotlin in handler

how to Convert java to kotlin in handler

new Handler().postDelayed(new Runnable(){
    @Override
    public void run() {
        /* Create an Intent that will start the Menu-Activity. */
        Intent mainIntent = new Intent(Splash.this,Menu.class);
        Splash.this.startActivity(mainIntent);
        Splash.this.finish();
    }
}, 3000);

Upvotes: 23

Views: 48734

Answers (3)

jonathan rojas
jonathan rojas

Reputation: 1

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    
    // Establecer el modo noche predeterminado en "Modo noche apagado" (MODE_NIGHT_NO)
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    new Handler().postDelayed(new Runnable(){
        public void run(){
            Intent intent = new Intent(getApplicationContext(), Login.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(intent);
            finish();

        }
    }, 3000); //5000 milliseconds = 5 seconds.
}

Upvotes: 0

Anand Nishad
Anand Nishad

Reputation: 126

 // Declare Duration of splash screen display (in milliseconds)
    private val SPLASH_DURATION = 2000L 

Inside - onCreate()

Handler().postDelayed({
           val intent = Intent(this, ActivityLogin::class.java)
            startActivity(intent)
           finish()
        }, SPLASH_DURATION)
    }

Upvotes: 0

RobCo
RobCo

Reputation: 6495

Java converted to Kotlin:

Handler(Looper.getMainLooper()).postDelayed({
    val mainIntent = Intent(this, Menu::class.java)
    startActivity(mainIntent)
    finish()
}, 3000)

If you are using the androidx-core-ktx package, there is an extension function which reorders the arguments to be more suitable for Kotlin:

Handler(Looper.getMainLooper()).postDelayed(1000) {
    // Do something
}

Upvotes: 72

Related Questions