Finish old activity and start a new one or vice versa

后端 未结 7 2117
轮回少年
轮回少年 2020-12-02 15:16

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

finish();
startActivity(newActivity);

and

startActivity(newActiv         


        
相关标签:
7条回答
  • 2020-12-02 15:32

    The animation is clearly different (at least on 4.1 onwards). Calling finish() first starts to fade away the first activity earlier and you can briefly see a black background before the new activity fades in. Calling startActivity() first fades in the new activity on top of the old one and the black background is not visible.

    0 讨论(0)
  • 2020-12-02 15:38

    I had the similar issue:

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

    here if I use:

    finish();
    startActivity(A);
    

    something wired happens: Activity B comes to foreground instead of A! but if I change the code like this:

    startActivity(A);
    finish();
    

    everything seems ok and Activity A comes visible.

    I don't know what's the problem, but it seems that in the first case, C is finished before executing the startActivity command so that the back stack handles the situation and shows its top activity which is B! but in the second case, everything happens normally.

    0 讨论(0)
  • 2020-12-02 15:39

    There is an important difference in application task behavior depending on the order of startActivity() and finish() invocations.

    The case I am describing is scoped only to the situation when the current activity (the one being stopped) is the only one in the task.

    Normally you would expect that the starting intent (the intent you create to start another activity) is not altered by the system. And that is not the case if finish() is called on the last activity in the task before calling startActivity().

    In this case the ActivityManager, a system component, while executing the startActivity() adds Intent.FLAG_ACTIVITY_NEW_TASK flag to your intent.

    When this happens, then one may notice a log entry in the LogCat similar to this one:

    W/ActivityManager: startActivity called from finishing ActivityRecord{4a19b47 u0 com.foo.bar/com.foo.bar.SplashActivity t4928 f}; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { cmp=com.foo.bar/com.foo.bar.MainActivity }

    And this is the turning point from which (under some conditions) things may go wrong.

    To sum up, if you want to be on a safe side (rather than experience unexpected side effects of the FLAG_ACTIVITY_NEW_TASK being added to the intent), then the order must be:

    • startActivity()
    • finish()

    Demo project.

    Screen recordings:

    • startActivity_finish.mp4 - works as expected.
    • finish_startActivity.mp4 - unexpected activity duplicate.
    0 讨论(0)
  • 2020-12-02 15:39

    I would do the second choice, I'm not backing this on anything that I have looked up from official sources, but, it makes more sense to launch the new activity before you call finish, that way the new activity pops up via an intent, and the now background activity can call all its cleaning up methods.

    If you were to do it the other way around, maybe the intent wont have time to fire before the cleaning up is done. I.e. will the activity call startActivity() after the finish() call?

    I hope you understand what I'm trying to state, I would do the second option just to be safe.

    0 讨论(0)
  • 2020-12-02 15:41

    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. So I don't see a big difference between the two.

    0 讨论(0)
  • 2020-12-02 15:42

    In addition to the Emmanuels answer:

    Both methods startActivity and finish will be scheduled after the end of the invoking method, since both are processed by the UI thread.

    0 讨论(0)
提交回复
热议问题