Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code still runs after startActivity()

My app lauches on a splash activity, if I don't have certain credentials in my database, it has to go a login page.

Intent intent = new Intent(Splash.this, Login.class);
startActivity(intent);
Log.d("debug", "code is still executing!!!");

Problem: after my startActivity(), it still runs all the code below. ("code is still executing" is printed in the console).

Goal: don't execute any code from splash activity, go to login activity.

like image 291
TomCB Avatar asked Aug 08 '14 17:08

TomCB


1 Answers

That's normal behaviour. startActivity() does not terminate current one nor instantly aborts/quits the code it is called from. It adds a new intent to the handler's queue for further processing, yet this intent won't be handled by the framework unless control is returned to the system event loop, which usually means unless your method ends executing.

If you really need to terminate current activity just call finish() in your method to tell the framework you done with this one. Note, again, that finish() does not terminates the current Activity instantly, so if you got code after finish() it will be executed. If that's not your intention, use i.e return; to return control to the framework just after finish() call.

Snippet from documentation:

void finish ()

Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

like image 169
Marcin Orlowski Avatar answered Sep 28 '22 09:09

Marcin Orlowski