Search code examples
androidandroid-activity

Is finish always scheduled before startActivity?


startActivity(newActivity);
finish();

Assuming I'm calling it like above. Both calls are scheduled to happen on the UI thread after the end of the invoking method. However, is there a particular order in the scheduling? Is finish always scheduled before startActivity? or vice versa?


Solution

  • When calling finish() on an activity, the method onDestroy() is executed this method can do things like:

    1. Dimiss any dialogs(search dialogs) that activity was managing.
    2. Close any cursors the activity was managing. And the activity is removed from the stack.

    And calling startActivity(newActivity) creates and puts a new View on the Top.

    Thus,if order is

    startActivity(newActivity);
    
    finish();
    

    Then first newActivity is displayed and old one is detroyed.

    But,if order is

    finish();
    
    startActivity(newActivity);
    

    Then first the existing activity is destroyed and new one is created and displayed.

    So, if we have lots of things to do in onDestroy()(like storing some datas) then calling startActivity() then finish() will be a good thing to do.Thus, the order depends on what we call first.