Skip to content Skip to sidebar Skip to footer

Thread Stopping Issue Android

I want to stop a running thread while click on the splash screen, if I don't click on screen, after the thread execution, it will launch another Activity. But getting UnSupportedEx

Solution 1:

What you're doing is very wasteful (any splash screen is wasteful, but using Threads like this is more so), but to fix your issue:

use interrrupt(); intead of stop();

As the docs say for stop()

Throws UnsupportedOperationException.

And to fix the duplicate issue, move the startActivity() inside the try so it looks like this:

publicvoidrun() {
  try {
    Thread.sleep(5000);
    startActivity(new Intent(SplashActivity.this, LoginAuthenticationActivity.class));
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  finish();
}

That way when you call interrupt() all your Activity does is finish() and the duplicate startActivity() is not called.

To further explain:

Very first issue: stop() throws an exception by default, since it's an unsafe method which you're not supposed to use.

Then when you used interrupt(), you had startActivity() in the run method after the catch block. When you interrupted, startActivity() was called once in run() and once in onClick(). By moving startActivity() inside the try block to right after Thread.sleep(), when interrupt() interrupts the Thread, the rest of the try block isn't executed. This means that you now only have one startActivity() call instead of two. For more information, read up on exceptions.

Post a Comment for "Thread Stopping Issue Android"