I have an android activity that needs to call another activity (to get a password from the user) before its own screen is brought up. My code currently does this by calling startActivity() from within onResume(); it sort of works, but I have been getting inconsistent behaviour. Can anyone tell me whether this approach is legal or not (and if not, how should I do it) ?
Thanks, Richard.
onResume() will never be called before onCreate() . Save this answer. Show activity on this post. onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() .
If you call startActivity() for an Activity with default launch mode(i.e, you didn't mention any launch mode in either in manifest or in Intent) a new instance of the activity is created. For example, A launched B and again B launched A then Activity stack would be A - B - A.
onResume() is called whenever you navigate back to the activity from a call or something else. You can override the onResume method similarly as onCreate() and perform the task. This may help you understand the lifecycle of and Android app more.
Calling startActivity()
in onResume()
is absolutely fine. I have many activities which do this, often in reaction to events which have happened while the activity was stopped.
All startActivity()
does is tell Android to start the new activity and add it to the top of the back stack when the main thread becomes available, which in this instance will be after Android has finished calling all the necessary lifecycle callbacks on the current activity.
If you are experiencing odd behaviour I doubt is related to this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With