Tolen
Tolen

Reputation: 41

start Splash screen activity

I want to write a code that uses the splash screen .I have written this so far, but Can anyone tell me what is the missing here!?

here is my main code:

package com.example.splash;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

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


    }



} 

and here is my splash activity code:

package com.example.splash;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class splashscreen extends Activity {

protected int _splashTime = 5000; 

    private Thread splashTread;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splashh);
             final splashscreen sPlashScreen = this; 

                splashTread = new Thread() {
                    @Override
                    public void run() {
                        try {                   
                            synchronized(this){
                                wait(_splashTime);
                            }

                        } catch(InterruptedException e) {} 
                        finally {
                            finish();

                            Intent i = new Intent();
                            i.setClass(sPlashScreen,MainActivity.class);
                            startActivity(i);

                            //stop();
                        }
                    }
                };


                splashTread.start();

    }

The problem is I do not know how to tell my main to go splash activity , if I use an intent I would stuck on infinite loop.

Upvotes: 1

Views: 7102

Answers (7)

Mabu Kloesen
Mabu Kloesen

Reputation: 1358

You can create a Thread for doing something or just sleep for a few seconds to do, such as

public class MainActivity extends Activity {

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

    Thread background = new Thread() {
        public void run() {

            try {
                // Thread will sleep for 3 seconds
                sleep(3*1000);

                // After 3 seconds redirect to another intent
                Intent i=new Intent(getBaseContext(),MenuActivity.class);
                startActivity(i);

                //Remove activity
                finish();

            } catch (Exception e) {

            }
        }
    };

    background.start();

}

@Override
protected void onDestroy() {

    super.onDestroy();

}

You can get more example here.

Upvotes: 0

Zar E Ahmer
Zar E Ahmer

Reputation: 34360

Full detail of creating a splash Activity

Create a layout for Splash

splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/splash" >


</LinearLayout>

Now create a class Under package . Name it Splash

public class Splash extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {

            Intent openMainActivity =  new Intent(Splash.this, MainActivity.class);
            startActivity(openMainActivity);
            finish();

        }
    }, 5000);  //it will call the MainActivity after 5 seconds  
}

Go to manifest and add the Activity to it.

and cut the intent-filter where main and Launcher are child and paste it in Splash Activity like

 <activity
         android:name="com.example.yourpackage.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>

Upvotes: 0

Piyush
Piyush

Reputation: 18933

You can simply use this:

 Handler handler=new Handler();
        handler.postDelayed(new Runnable()
        {               
            @Override
            public void run() 
            {
                Intent intent = new Intent(SplashViewController.this,HomeViewController.class);
                startActivity(intent);
                SplashViewController.this.finish();                         
            }
        }, 3000);

Upvotes: 3

Jibran Khan
Jibran Khan

Reputation: 3256

Try to change your SplashActivity code from here.

Splash and main activity error

Also make your splashactivtiy as your launcher activity and then redirect to the MainActivity from the SplashScreen

<activity
            android:name="com.app.wablogic.SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>

Upvotes: 1

tj&#39;s
tj&#39;s

Reputation: 144

Try this code:

private boolean _active = true;
private int _splashTime = 5000;

Thread splashTread = new Thread() 
        {
            @Override
            public void run() 
            {
                try 
                {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) 
                    {
                        sleep(100);
                        if(_active) 
                        {
                            waited += 100;
                        }
                    }
                } 
                catch(InterruptedException e) 
                {
                    e.printStackTrace();
                } 
                finally 
                {
                        Intent intent = new Intent(SplashScreenActivity.this,MainActivity.class);
                        startActivity(intent);
                    finish();
            }
        };
        splashTread.start();

in AndroidManifest mention your activity as Main Activity.

Upvotes: 1

AggelosK
AggelosK

Reputation: 4341

The problem (i guess) is that your app is starting with your MainActivity as your launcher Activity. Make splashscreen your laucher Activity in your Application Manifest XML and you will avoid the infinite loop.

Upvotes: 1

Muhammad Omer
Muhammad Omer

Reputation: 229

try this instead :

public class splashscreen extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_splash);
            Thread t = new Thread(Splash_Runnable);
            t.start();

        }





        Runnable Splash_Runnable = new Runnable() {

            @Override
            public void run() {
                try {
                    Thread.sleep(3000);

                        startActivity(new Intent(splashscreen.this,
                                MainActivity.class));
                        splashscreen.this.finish();

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        };

    }

Upvotes: 1

Related Questions