End the old activity and start a new one or vice versa

I know that I get the same result with both code snippets

finish(); startActivity(newActivity); 

and

 startActivity(newActivity); finish(); 

I would like to know your opinion if there is a big difference between them. Better than the other? If so, why?

+57
android android-activity
Nov 15 '10 at 8:56
source share
6 answers

When you execute startActivity (), everything that does sends your intention to the event queue. The actual start of activity occurs asynchronously in the near future. Therefore, I do not see much difference between the two.

+57
Nov 15 '10 at 15:29
source share

The animation is clearly different (at least starting with 4.1). The call to finish() first starts to fade out the first activity earlier, and you can briefly see the black background before the new look starts to work. The startActivity() call first disappears in the new action on top of the old, and the black background is not visible.

+18
May 21 '14 at 9:02
source share

I would make a second choice, I don’t support it for anything that I was looking from official sources, but it makes sense to start a new activity before you name the finish, so a new action appears with the intention, and now the background activity can cause all his cleaning methods.

If you had done it the other way around, perhaps the intention would not have time to fire before the cleaning is over. That is, will the startActivity () activity call after the finish () call?

I hope you understand what I'm trying to formulate, I would make the second option to be safe.

+6
Nov 15 '10 at 15:21
source share

In addition to Emmanuel's answer:

Both startActivity and finish methods will be scheduled after the end of the calling method, since both of them are handled by the user interface thread.

+6
Jul 03 '13 at 9:13
source share

I had a similar problem:

 Activity A: singleInstance Activity B: singleInstance Activity C: singleInstance A starts BB starts C C wants to start A: 

here if i use:

 finish(); startActivity(A); 

something wired happens: activity B comes to the fore instead of A! but if I changed the code as follows:

 startActivity(A); finish(); 

everything looks normal and activity A becomes visible.

I don’t know what the problem is, but it seems that in the first case, C ends before the startActivity command is executed, so the back stack processes the situation and shows its top activity, which is b! but in the second case, everything happens fine.

+1
Dec 28 '13 at 11:25
source share

I usually do startActivity() before finish() , since I think that make sure the new screen comes out before the previous one goes blank.

I have a login page to my application. After a successful login, the login activity will disappear and the main action will be completed. It works great in Android 4.

Today I wanted to rewrite it in Material Design. However, I had a big problem. The new Android studio creates empty work with a material design, which, I think, requires a lot of resources. The same process, but I got an error

 11-26 18:20:44.450 18397-18397/? I/Choreographer: Skipped 42 frames! The application may be doing too much work on its main thread. 11-26 18:20:44.485 18397-18408/? I/art: Background partial concurrent mark sweep GC freed 2864(191KB) AllocSpace objects, 4(43MB) LOS objects, 13% free, 100MB/116MB, paused 8.056ms total 39.767ms 

He said that my applications take up a lot of resources when mainActivity runs in my phone logcat. I have nothing in mainActivity just that this is the default Material Design layout.

I changed the order and now it works without errors on my phone.

0
Nov 26 '15 at 10:47
source share



All Articles