Home > other >  How to use Intent in postDelayed - Android
How to use Intent in postDelayed - Android

Time:11-18

This is for a splash screen. I've followed the tutorial but it's still not working. It keeps on getting error for some reason. I've tried everything that I know but it still doesn't work. Please help.

This is my code:

package id.ac.umn.finalproject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class);

        new Handler().postDelayed(startActivity(startApp), 3000);
    }
}

CodePudding user response:

In Kotlin Try This

Handler(Looper.getMainLooper()).postDelayed({ Intent startApp = new Intent(MainActivity.this, PemasukanActivity.class); startActivity(startApp) }, 3000)

CodePudding user response:

The correct way to create a Splash screen is as follows

style.xml

 <style name="SplashStyle" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="android:windowBackground">@drawable/splash</item>
    </style>

Splash.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/black" /> // background color

    <item
        android:drawable="@drawable/ic_launcher" /> // logo
        android:gravity="center" />

</layer-list>

Manifest

<activity
            android:name=".SplashActivity"
            android:exported="true"
            android:noHistory="true"
            android:theme="@style/SplashStyle">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        </activity>

SplashActivity.java

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_splash); // No need setContentView
        
        Intent intent = new Intent(SplashActivity.this,MainActivity.class);
        startActivity(intent);
    }

CodePudding user response:

Try this:

int SPLASH_TIME_OUT = 3000;
/*
 * Showing splash screen with a timer. This will be useful when you
 * want to show case your app logo / company
 */
new Handler().postDelayed(() -> {
    // This method will be executed once the timer is over
    // Start your app main activity
    Intent i = new Intent(SplashScreenActivity.this, HomeActivity.class);
    startActivity(i);

    // close this activity
    finish();
}, SPLASH_TIME_OUT);   

It works fine for me.

  • Related