I strongly recommend not calling it from onStop() or onDestroy() -- those are called well after the user has left the activity, so if you are starting a new one at that point you have no idea what the state of the UI is at that point. There is actually no timeliness guarantee for these -- you know that onStop() will be some time after your activity, and onDestroy() some time after it is no longer in use, but there is no rush the system makes to call them. And anyway... it doesn't really make sense semantically to start a new activity at the point where your activity is no longer visible (because by definition something else is now in front of the user), let alone when it is destroyed.
On Tue, May 3, 2011 at 10:40 PM, Eric <[email protected]> wrote: > Of the lifecycle methods, like onCreate(), onStart(), onResume(), can > startActivity() be called from inside those methods? If not, what > would happen if you tried it? If so, how would calling > startActivity() affect the current Activity's lifecycle? If you call > startActivity() from onStart(), for example, before an Activity is > displayed to the user, does the current Activity skip right to > onStop(), before showing the new Activity, or would it's normal > lifecycle happen (onResume(), onPause(), onStop(), then new activity)? > > I would like to ask the same question of finish(). Can finish() be > called from inside any/all of the Activity lifecycle methods? The > documentation refers to calling finish() from onCreate(), where it > talks about the normal lifecycle being interrupted as a special case: > > "You can call finish() from within this function, in which case > onDestroy() will be immediately called without any of the rest of the > activity lifecycle (onStart(), onResume(), onPause(), etc) executing.' > > > I'm just curious what happens in these special cases, and whether > there are any caveats to calling these methods in these areas. > > One reason I ask is because I am trying to figure out the best way to > present a login panel (either a dialog or a LoginActivity) to the user > whenever any of my Activities are entered into, but the system hasn't > yet logged the user onto the server. There are probably countless > ways to achieve this functionality, and I'm trying to determine the > most recommended way. > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- Dianne Hackborn Android framework engineer [email protected] Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails. All such questions should be posted on public forums, where I and others can see and answer them. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

