MBMJ
MBMJ

Reputation: 5431

Splash Image for android

I made a splash image to show at the start of my activity.. The image show perfectly.But the problem is when i call this

public class SplashImageActivity extends Activity {
    protected boolean active = true;
    protected int splashTime = 5000; // time to display the splash screen in ms

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

        // thread for displaying the SplashScreen
        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) {
                    // do nothing
                } finally {
                    startActivity(new Intent(SplashImageActivity.this,Myapps.class));
                    finish();
                    //startActivity(new Intent("com.splash.com.MyApps"));
                    //startActivity( new Intent(getApplicationContext(), Myapps.class));
                }
            }
        };
        splashTread.start();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            active = false;
        }
    return true;
    }
}

go for next activity the stop() does not work. And it does not go to this activity. I add all activity in manifest. The stop() shows in code like this

enter image description here

what's the problem?

Upvotes: 2

Views: 481

Answers (5)

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

first thing it is not onStop of Activity so looks you are calling stop function of thread which is Deprecated that's why you are getting the strike line so use other way to stop the thread of use better way to implement the splash ........

as looks you try some thing like this link

Upvotes: 0

deepa
deepa

Reputation: 2494

No need to call stop() and call finish() after starting activity

finally 
{

    startActivity(new Intent(currentclass.this,nextActivity.class);
    finish();
}

Upvotes: 5

K_Anas
K_Anas

Reputation: 31456

No need to call stop() just call finish() after starting activity

finally {
startActivity(new Intent(currentclass.this,nextActivity.class);
finish();
}

You can also use handler an postdelayed() to make a splash screen like below

 public class SplashScreenActivity extends Activity{

        private Handler handler;

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

            final Runnable runnable = new Runnable() {

                @Override
                public void run() {
                       Intent intent=new Intent(SplashScreenActivity.this, nextActivity.class);
                       startActivity(intent);
                       finish();

                }
            };
            handler = new Handler();
            handler.postDelayed(runnable, 5000);


        }

    }

You will show your splash screen for 5 seconds and then move to next Activity

Upvotes: 0

Yogesh Somani
Yogesh Somani

Reputation: 2624

I use thread to show the Splash screen, and it works for me:

       @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    mSplashThread =  new Thread(){
        @Override
        public void run(){
            try {
                synchronized(this){
                    wait(4000);
                }
            }catch(InterruptedException ex){                   
            }
            finish();

            Intent i=new Intent(getApplicationContext(),NextActivity.class);
            startActivity(i);

            interrupt();
        }

    }; 
    mSplashThread.start();        
}

Upvotes: 2

Nikhil
Nikhil

Reputation: 16194

Please try below code..

public class Splashscreen extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Thread t2 = new Thread() {
                public void run() {
                    try {
                        sleep(2000);
                        startActivity( new Intent(getApplicationContext(), Exercise.class));
                        finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            t2.start();
        }
    }

Upvotes: 1

Related Questions