简体   繁体   中英

Android app reopening and proceeding on the next activity even when pressing the home button or back button

I wrote this app that in the first screen it has an included Thread on it. So it has I timed it like 7 seconds then it will proceed to the next activity.

The problem is whenever I hit the home button the music will stop and it will go to android homescreen but after my timed is done which is the 7 seconds, the app will reappear and will show the next activity.

I tried putting finish(); in the onpause(); but it's still showing the next activity.

here's the actual code.

public class HelloWorldActivity extends Activity {
    MediaPlayer mp;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        mp = MediaPlayer.create(this, R.raw.otj);
        mp.start();

        Thread LogoTimer = new Thread(){
            public void run(){
                try{
                    int LogoTimer = 0;
                while(LogoTimer < 7000){
                    sleep(100);
                    LogoTimer = LogoTimer + 100;


                    }


                startActivity(new Intent("com.example.HelloWorld.CLEARSCREEN"));
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }

                    finally{
                    finish();
                    }

            }
        };
        LogoTimer.start();

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        mp.release();



    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        mp.pause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }
}

First, that's a really inefficient way to run a timer. Try this way instead:

new Handler().postDelayed(new Runnable() {

    public void run() {
        // Do some work.
    }

}, delayTimeInMs);

Second, your starting a new activity when that timer eventually fires. It doesn't matter that the originating activity is finished. Your startActivity() is running on it's own thread and will execute regardless.

It's possible the postDelayed() method will function like you expect. If not you'll need to have it check when it runs whether it should really start the activity. However, I think the Handler is attached to the default Looper which means it will stop (or rather, the message won't be posted) if the main activity finishes.

The application is still in the background and the thread is not destroyed so it will fire the startActivity.

I would not really setup a splash screen this way, or use a thread unless I wanted it off the UI for some reason, even then there are better options.

For educational purposes to take care of this you need to be able to abort the thread safely in onPause() one way to do so is below

Modifed Thread

    Thread LogoTimer = new Thread() {

    private volatile boolean abortThread = false;

       public void run(){

       long stopAt = System.currentTimeMillis() + 7000;

       while (!abortThread && stopAt > System.currentTimeMillis())
          yield();

       if (!abortThread)
           startActivity ... 
     }

    public synchronized void stopThread() {
        abortThread = true;
    }
};

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM