Android: Splash Screen re-opens app to second page even if app is quit during splash screen

社会主义新天地 提交于 2019-12-03 23:17:53

问题


Ok, so i created a very simple splash screen using this tutorial:

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

The problem is that if the user hits the home button or the back button to exit the app during the splash screens duration then the app will re-open to the second screen after the splash screen would have been done if the user did no exit.

My code is pretty much that of the tutorial. any help?

Thanks


回答1:


I have modified the code to make use of the lifecycle methods better. enjoyed changing it. :)

   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();
            }

        }
    }

My point of view is that the use of Splash Screen is not frequent but maybe needed. If you are doing heavy work behind the screen( such as games).



来源:https://stackoverflow.com/questions/9791570/android-splash-screen-re-opens-app-to-second-page-even-if-app-is-quit-during-sp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!