loading...

Android interview: Activity Lifecycle

mauriciomunoz profile image Miguel Munoz Updated on ・2 min read

Following with my post about all those android interview questions, this is one of the most important and in most of cases recruiters from Facebook or other top companies uses to evaluate a candidate to know if is worth it to send him to the technical interview.

What is the lifecycle of an Activity?

Well, the lifecycle is the path of actions that an activity follows from start to finish.

These actions are the next methods:

  • OnCreate() - Called when the activity is created. This callback is called once in all the lifecycle. Use this when you need to initialize data or do something just once.

  • OnStart() - This is called when the activity is visible to the user.

  • OnResume() - After OnStart() is finished, the app enter into OnResume(). This is the state where the app interacts with the user. If any event interrupts the app (receiving a phone call, move to another activity, screen turn off) the state will move to OnPause() if the user goes back to the app, OnResume() will be called again, that’s why here is just necessary to initialize the objects released in OnPause() state.

  • OnPause() - This method is called when the user left your activity, but this doesn’t mean the activity is destroyed. It just mean the activity is not in the foreground like when a Dialog appears or some event interrupts the app execution. You can use this state to free resources like GPS or any resource that could be affect the battery life.

  • OnStop() - Is called when the activity is not longer visible to the user. Here you can stop animations to free resources. After goes back from OnStop() the OnRestart() method will be called.

  • OnRestart() - It comes from OnStop() state and is followed by OnStart().

  • OnDestroy() - This method is called if Android have low memory and needs to free resources. Also can be called using Finish().

If any of my post have mistakes, I am open to any feedback. Sometimes I could do bad explanation, so any comment is welcome. Together we can create a better guide to study and learn.

Discussion

pic
Editor guide