Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I call startActivity() from within onResume()?

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.

like image 589
user1163984 Avatar asked Jan 22 '12 22:01

user1163984


People also ask

Is onResume called after onCreate?

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() .

What happens when an application calls startActivity for an activity that already has an instance running?

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.

When onResume method is called in Android?

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.


1 Answers

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.

like image 100
tomtheguvnor Avatar answered Sep 22 '22 01:09

tomtheguvnor