Reputation: 232
Hi I have been searching for ways to get my android app to load StartActivity.java for 5 seconds and then load into MainActivity.java once 5 seconds is up. Any examples i have found havent worked for me. So wondering if anyone can point me in the right direction.
Any help would be appreciated.
StartActivity.java
package com.example.testerrquin.euro2016fanguide;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class StartActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
}
}.start();
}}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>`<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testerrquin.euro2016fanguide" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".StartActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_start"
android:theme="@style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Thanks in advance for any answers.`
Upvotes: 2
Views: 1345
Reputation: 29
Just remove the finish() and try it again
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent startNewActivity = new Intent(CurrentActivity.this, MainActivity.class);
startActivity(startNewActivity);
}
}, 5000);
Upvotes: 0
Reputation: 2126
Try this out. It works in my application.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent start = new Intent(StartActivity.this, MainActivity.class);
startActivity(start);
finish();
}
}, 5000);
Upvotes: 6
Reputation: 5087
Try this
public class StartActivity extends AppCompatActivity {
protected int SECONDS = 5;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
handler.removeCallbacks(runnable);
handler.postDelayed(runnable, 1000);
}
private Runnable runnable = new Runnable() {
public void run() {
long currentMilliseconds = System.currentTimeMillis();
SECONDS--;
if (SECONDS > 0) {
handler.postAtTime(this, currentMilliseconds);
handler.postDelayed(runnable, 1000);
} else {
Intent it = new Intent(StartActivity.this, MainActivity.class);
startActivity(it);
handler.removeCallbacks(runnable);
finish();
}
}
};
}
The main tricks is made by using handler.postDelayed
Hope its helps!!
Upvotes: 1