Android:即使应用程序在启动画面期间退出,启动画面也会重新打开应用程序到第二页

时间:2012-03-20 17:07:50

标签: android splash-screen

好的,所以我使用本教程创建了一个非常简单的启动画面:

http://p-xr.com/android-tutorial-how-to-make-a-basic-splash-screen/

问题是如果用户在启动屏幕持续时间内点击主页按钮或后退按钮退出应用程序,那么如果用户执行了启动屏幕,应用程序将重新打开第二个屏幕没有退出。

我的代码几乎就是本教程的代码。有什么帮助吗?

由于

1 个答案:

答案 0 :(得分:2)

我修改了代码以更好地利用生命周期方法。喜欢改变它。 :)

   public class SplashScreen extends Activity {

        protected int _splashTime = 5000; 

        private Thread splashTread;

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


            final SplashScreen sPlashScreen = this; 

            // thread for displaying the SplashScreen
            splashTread = new Thread() {
                @Override
                public void run() {
                    try {                   
                        synchronized(this){
                            wait(_splashTime);
                        }

                    } catch(InterruptedException e) {} 
                    finally {

                        if(!isFinishing()) // This pretty useful boolean val tells if 
 //user has pressed the back button. very useful.
                        {Intent i = new Intent(SplashScreen.this, Main.class);

                        startActivity(i);
                         finish();
                         }


                        stop();
                    }
                }
            };

            splashTread.start();
        }
        @Override
        public boolean onTouchEvent(MotionEvent event) {


            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                Toast.makeText(this,"exec---",Toast.LENGTH_LONG).show();
                synchronized(splashTread){
                    splashTread.notifyAll();
                }
            }
            return true;
        }
        @Override
        protected void onPause() {

            super.onPause();

            if(splashTread.getState()==Thread.State.TIMED_WAITING){
                //Thread is still waiting and Activity is paused. Means user has pressed Home. Bail out
                finish();
            }

        }
    }

我的观点是,Splash Screen的使用并不频繁,但可能需要。如果你在屏幕后面做了大量的工作(比如游戏)。