Home > Mobile >  Handler postDelayed method not working, kotlin app stuck on splash screen
Handler postDelayed method not working, kotlin app stuck on splash screen

Time:11-10

I'm trying to use the code below to briefly display spalsh screen for about 3seconds before opening the LandingPage activity. But so far the app has only been stuck on the splash screen. Also using the supportActionBar?.hide() method doesn't hide the action bar from this activity. please what can i do?

package com.example.dialogsinandroid

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.appcompat.app.AppCompatActivity

class SplashScreen : AppCompatActivity() {

    lateinit var handler: Handler
    override fun onCreate(savedInstanceState: Bundle?) {

        supportActionBar?.hide()

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash_screen)

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

Here's the application manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.DialogsInAndroid"
        tools:targetApi="31">
        <activity
            android:name=".SplashScreen"
            android:theme="@style/Theme.AppCompat.NoActionBar"
            android:exported="false">
            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>

        <activity android:name=".LandingPage"
            android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"/>

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

I've tried researching many methods of using the Handler postDelayed method, it still won't work.


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

CodePudding user response:

I think you should move

<intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

from MainActivity tag to SplashActivity tag.

And change Theme in application tag and SplashActivity tag to :

android:theme="@style/AppTheme"

//and
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
  • Related