I'am new to android , so please bear with me. I'am trying to display another activity after a timer of 5 seconds, but it doesn't show up. I've made sure both the activities are in the manifest file.
Splash.java
:
package com.example.pournima.metallica;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run() {
try{
Thread.sleep(5000);
}catch(Exception e){
}finally{
Intent in = new Intent(Splash.this,MainActivity.class);
startActivity(in);
}
}
};
}
}
MainActivity.java
:
package com.example.pournima.metallica;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pournima.metallica" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.pournima.TheStart" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Add timer.start();
as
Thread timer = new Thread(){
public void run() {
try{
Thread.sleep(5000);
}catch(Exception e){
}finally{
Intent in = new Intent(Splash.this,MainActivity.class);
startActivity(in);
}
}
};
timer.start();
add timer.start()
Once you create a thread , you need to start it too ^^
public void run() {
try{
Thread.sleep(5000);
}catch(Exception e){
}finally{
Intent in = new Intent(Splash.this,MainActivity.class);
startActivity(in);
}
}
};
timer.start();
You can create handler as well instead thread as follows,and make sure you have to finish splash activity as it is splash screen
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, 1000);
remove timer thread and add this code
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(Splash.this,MainActivity.class));
}
}
}, 5000);
You just forgot to start the thread. So just call timer.start();
Add below code to your Splash.java class.
Thread timer = new Thread(){
public void run() {
try{
Thread.sleep(5000);
}catch(Exception e){
}finally{
Intent in = new Intent(Splash.this,MainActivity.class);
startActivity(in);
}
}
};
timer.start();
}
}
try like this
in splash Activity class:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
getSplash();
}
In getSplash() method :
private void getSplash() {
Handler welcome_handler = new Handler();
Thread thread = new Thread() {
public void run() {
try {
sleep(Constants.SPALASH_TIMMING);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
finish();
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
}
};
thread.start();
}
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.