简体   繁体   中英

Why doesn't my countdown timer switch to my other activity?

I have a countdown timer that automatically switches from one activity (home activity) to my another activity(home). The problem is that the code will count but wouldn't display my 2nd activity, and it would go back to my main activity. Does anyone know the solution to this?

This is my code for my timer:

package com.login.register;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import java.util.Timer;
import java.util.TimerTask;

public class HomeActivity extends AppCompatActivity {
    Timer timer;
    //---------------------------Timer--------------------------------------------------------------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Intent t = new Intent(HomeActivity.this, Home.class);
                startActivity(t);
                finish();
            }
        }, 3000);}}

Here is my Android Manifest:

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

    <application

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Login">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".HomeActivity"/>
        <activity android:name=".MainActivity">
        </activity>
    </application>
</manifest>

Add below code Inside onCreate() of first activity:

Kotlin Code:

Handler().postDelayed(Runnable {
            startActivity(Intent(this@HomeActivity,Home::class.java))// Home class should be an activity and should be declared in Manifest file 
        },5000)//5 sec delay

Java Code:

new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(HomeActivity.this, Home.class)); //Home class should be an activity and should be declared in Manifest file 
            }
        },5000); // 5 sec delay

java.util.Timer and android.os.Handler both are kinda Thread but handler is much faster than timer in term of performance. see this diff b/w handler and timer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM