4

First way

Intent in = new Intent(VerificationActivity.this, VerifyCode.class);
in.putExtra("verificationCode", verificationCode);
finish();
startActivity(in);

Second way...

Intent in = new Intent(VerificationActivity.this, VerifyCode.class);
in.putExtra("verificationCode", verificationCode);
startActivity(in);
finish();

which is better way? is there any difference ?

Tim
  • 41,901
  • 18
  • 127
  • 145
Android007
  • 155
  • 11
  • I checked ....... thnks..... tell one more things performance is different or both are same.... – Android007 Oct 20 '15 at 09:38
  • 1
    If you had checked it as you said, you would have known already. No it does not matter – Tim Oct 20 '15 at 09:41
  • ok.... actually i want to know more deeply at system call level..... but i read its not effect performance in both case ..... but both methods way of process is different.... as in the Answer of your commented link [link](http://stackoverflow.com/questions/4182761/finish-old-activity-and-start-a-new-one-or-vice-versa) is " When you do startActivity(), all that does is post your intent in a queue of events. The actual starting of the activity happens asynchronously in the near future. " Means performance matters – Android007 Oct 20 '15 at 09:50
  • No performance does not matter in a calling order with this little impact, don't worry about it unless it's giving you problems. *premature optimization is the root of all evil* – Tim Oct 20 '15 at 09:54

2 Answers2

2

It doesn't matter which way you call them, startActivity() and finish() will not happen instantly anyway. Your method will always finish all of the code within it's block before closing unless you call return.

vguzzi
  • 2,420
  • 2
  • 15
  • 19
  • is it documented anywhere? – Weishi Z Sep 02 '16 at 08:11
  • I can't find the specifics at the moment. But the instance won't be closed if a code block is still running. If you need to check if something has been executed add logic inside onDestroy() to see if what your concerned about has definitely been executed. Calling finish() is only telling your Activity to finish when it is ready and cleaned up. – vguzzi Sep 02 '16 at 10:48
1

finish() call call the onStop() the current activity and startActivity(i) moves the new intent i to the event queue. and activity is launched when its taken outta the event queue . in the former if the event queue is stalled you'll see the current activity vanishing and after some time when intent gets its turn .. the activity would appear out of nowhere. but thanks to the Android's scheduling this almost never happens.

Minato
  • 4,383
  • 1
  • 22
  • 28
  • 1
    Is it possible that `finish()` leads to destruction of current activity instance before `startActivity(in);` happens? – Weishi Z Sep 02 '16 at 08:23