30

I've got an application. I use startActivity() to start activity.

Can anyone actually tell me why system is calling onStart() of new Activity first, instead of parents onStop()? Is that even possible (without system bug)?

I've found Fragment onStop() being called directly after onStart() - WHY? answer, but I got nothing in common with Fragments and using android-support library. I'm stuck because I'm using RoboSpice and it must contain proper, synchronized methods in onStart and onStop. I can't because system is calling it in wrong order.

I'm using GCM and Analytics libraries as well in this application.

Community
  • 1
  • 1
cadavre
  • 1,334
  • 1
  • 18
  • 34

1 Answers1

32

If you have a read of the Activity Lifecycle documentation onStop is only called when the current activity is replaced by a new (or previous) one.

For that to happen the other application has to start or resume... otherwise there would be a gap

onPause of the current activity is (I would expect) called before the onStart of the replacing Activity

Paul D'Ambra
  • 7,629
  • 3
  • 51
  • 96
  • 6
    Now I see, reading documentation for a 2nd, 3rd (and so on) time can always bring something new to knowledge. `onStop()` is "called when the activity is no longer visible to the user, because another activity **has been resumed**" – cadavre Jun 20 '13 at 13:21
  • 2
    It took me quite a while to spot that onPause could be more useful than onStop too :) – Paul D'Ambra Jun 20 '13 at 13:31
  • 1
    Yes, playing between `onPause()` and `onStop()` may end up in funny ways. I know that putting RoboSpice's `start()` and `shouldStop()` to `onResume()` and `onPause()` may not be a great idea, but it's only way (for now) to have only one instance of RoboManager shared between Activities. (for a future answers) – cadavre Jun 20 '13 at 14:01
  • 1
    Also, in the documentation of the onPause() function, they say: "After receiving this call (ndlr "onPause()") you will usually receive a following call to onStop() (after the next activity has been resumed and displayed)". – JonasVautherin Feb 27 '15 at 15:02