Slim C. Slim C. - 12 days ago 5
Android Question

Finishing Activity doesn't stop thread Android

I have splash activity and 1 thread. Thread starts timer and after some time main activity will start.
Unlike on other apps I don't want to disable backPressed button in Splash Activity. I want when backpressed is pressed to cancel thread and finish activity. But I can't get it to work.
Here is my code:

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

Thread Logo = new Thread() {
public void run() {

try {

sleep(1 * 1500);

Intent i = new Intent(getBaseContext(),
MainActivity.class);
startActivity(i);

finish();

} catch (Exception exception) {

}
}
};

Logo.start();

}
...


@Override
public void onBackPressed() {
Thread.currentThread().interrupt();
super.onBackPressed();
this.finish();
}
}


But this doesn't stop thread, it only finish activity and thread keeps running in background(and ofc starts activity)

M D M D
Answer

Make Thread Logo Object Globally and do Logo.interrupt();

You should stop the thread that you already Started before. and also as @TmKVU answer's Thread.currentThread(); return main UI/Main Thread but you have to Stop your Logo Thread.

Comments