4
  1. I have splash screen .
  2. once i open my application the splash screen will appears after completion of splash screen passed intent to HomeActivity.
  3. but when i kill this app while splash screen running after some time HomeScreen will automatically open , but i want to kill the app.
  4. but the HomeScreen should not show when i killed the app .

public class SplashAnimation extends Activity {

    ImageView imageViewSplash;
    TextView txtAppName;
    RelativeLayout relativeLayout;
    Thread SplashThread;
    MediaPlayer mySong;


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


        mySong=MediaPlayer.create(SplashAnimation.this,R.raw.monn);
        mySong.start();
        imageViewSplash = (ImageView) findViewById(R.id.imageViewSplash);
        txtAppName = (TextView) findViewById(R.id.txtAppName);
        relativeLayout = (RelativeLayout) findViewById(R.id.relative);

        startAnimations();
    }

    private void startAnimations() {

        Animation rotate = AnimationUtils.loadAnimation(this, R.anim.translate);
        Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);

        rotate.reset();
        translate.reset();
        relativeLayout.clearAnimation();

        imageViewSplash.startAnimation(rotate);
        txtAppName.startAnimation(translate);
        SplashThread = new Thread() {
            @Override
            public void run() {
                super.run();
                int waited = 0;
                while (waited < 3500) {
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    waited += 100;
                }
                SplashAnimation.this.finish();
                Intent intent = new Intent(SplashAnimation.this, LibraryView.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intent);
                mySong.stop();
            }
        };
        SplashThread.start();
    }

    @Override
    protected void onStop() {

        SplashAnimation.this.finish();
        finish();
        mySong.stop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        finish();
        mySong.stop();
        super.onDestroy();
    }
}
procra
  • 535
  • 4
  • 29
PRP
  • 101
  • 9

3 Answers3

1

Once you have called SplashThread.start() it will do its job as long as it can do. I would recommend to use a Handler instead, tho you can remotely cancel the task, the Handler runs:

//init and declare the handler instance
private Handler delayHandler;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (delayHandler == null) {
        delayHandler = new Handler();
    }
    //your code
}

//define the task the handler should do
private void startAnimations() {
//replace the code beginning at 'Thread SplashThread = new Thread()' with the following
delayhandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        Intent intent = new Intent(SplashAnimation.this, LibraryView.class);
        //these flags will prevent to 'redo' the transition by hitting the back button, that also makes calling 'finish()' obsolete
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);            
        startActivity(intent);
    }
//instead of the while loop just execute the runnable after below given amount of milliseconds
}, 3500)

//to remotely cancel the runnable, if the app, respectively the Activity gets killed override 'onDestroy()'
@Override
public void onDestroy() {
    super.onDestroy();
    mySong.stop();
    //calling 'finish()' is obsolete, tho 'finish()' calls 'onDestroy()' itself
    //tell the handler to quit its job
    delayHandler.removeCallbacksAndMessages(null);
}
procra
  • 535
  • 4
  • 29
  • Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Handler.removeCallbacksAndMessages(java.lang.Object)' on a null object reference at com.monnfamily.libraryapp.UIActivity.SplashAnimation.onDestroy(SplashAnimation.java:94) – PRP Dec 13 '18 at 12:47
  • delayHandler.removeCallbacksAndMessages(null); – PRP Dec 13 '18 at 12:47
  • have you defined delayHandler inside `onCreate()`? – procra Dec 13 '18 at 13:26
  • Yes i have initialised – PRP Dec 13 '18 at 13:27
  • i just noticed, that I had a typo in my answer. Make sure it's delayHandler everywhere (and not delayhandler with small h). If this doesn't work either simply write `private Handler delayHandler = new Handler();` and leave out the definition in `onCreate()` – procra Dec 13 '18 at 13:28
1

Call in onStop() method

SplashThread.interrupt()
Abdul Aziz
  • 442
  • 5
  • 12
0

You can use Timer instead of instantiating the Thread class.

Refer the code below to start the Activity after 4 seconds. Use this in onCreate() of SplashActivity.

timer = new Timer().schedule(new TimerTask() {          
    @Override
    public void run() {
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
    }
}, 4000);

In your onPause() method use:

timer.cancel()

This will terminate the timer and disregards any currently scheduled tasks.

sats
  • 647
  • 6
  • 17