Android activity and lifecycle
Tags: java, android, beginners, tristan
Published: 2021-06-08 03:17:00

#Introduction
- This series is going to be dedicated to the basic to Android development. Join me and let us try to build and understand some cool stuff. I used the [Android documentation](https://developer.android.com/guide/components/activities/activity-lifecycle#save-simple,-lightweight-ui-state-using-onsaveinstancestate) for all the information in this post. 

**Quick Note**: any time I reference `system` I am talking about the Android system, which is a modified Linux kernel system.


###What is an activity?
- Consisting of an .XML file for the UI and a Java file to handle the logic, an activity is a crucial part of any android app. Unlike a normal Java application, an Android app does not have a main method to act as a starting point. Instead the Android System(modified Linux kernel system) uses `activities` to run code. The code that is being run is specified to run in certain stages of the `activitie's lifecycle`.

###What is  the activity lifecycle?
- As the user navigates through individual activities or navigates from one activity to another. The activities go through a `lifecycle`, as they go through this `lifecycle` they trigger specific callback methods at points in the lifecycle called stages. These callback methods dictate how the activate behaves when the certain stage is reached.

- Each activity and its lifecycle are stored on a `backstack` which operates on the traditional stack logic of first in, last out. It is thanks to this `backstack` that we are able to use the back button to transition through our previous activities.

- To handle the transitions between states in the activity lifecycle, the Activity class provides us with 7 methods.

**1) onCreate()** : this method is mandatory and must be implemented when creating an activity. It is called when the activity is first created, this method is where we should create views(UI) and bind data to lists. It also provides us with a `Bundle` variable which is the activities previous state, if there is any.

**2) onStart()** : this method is called when the activity becomes visible to the user. It is a good place to begin drawing visual elements and running animations.

**3) onResume()** : this method is called when the activity will start interacting with the user. At this point the activity is at the top of the `backstack`. The activity will stay in this state until something happens to take focus away from the activity.

**4) onPause()** : always called after onResume() and when the user is no longer actively interacting with the activity but the activity is still visible on the screen.

**5) onStop()** : this is called when the activity is no longer visible to the user. This may happen because the activity has been  destroyed or a new activity is starting.

**6) onRestart()** : called when the current activity is being redisplayed to the user. This callback is followed by a call to the onStart() method.

**7) onDestroy()** : is called before the activity is destroyed. The system invokes this callback either because, the user dismisses the activity, or there is a configuration change(device rotation). 


#References 

[Activity Documentation](https://developer.android.com/reference/android/app/Activity)

[Intro to activities](https://developer.android.com/guide/components/activities/intro-activities)

[Activity lifecycle video](https://www.youtube.com/watch?v=qgnKr6-XbcY&t=124s)

[Understanding Activities LifeCycle](https://developer.android.com/guide/components/activities/activity-lifecycle)

[Backstack](https://developer.android.com/guide/components/activities/tasks-and-back-stack)



#Conclusion
- Thank you for taking the time out of you day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on [Twitter](https://twitter.com/TristAndroidDev).
- Also make sure to checkout my [YouTube](https://www.youtube.com/channel/UCN9jWE9COJ0wN1m4huDFqzw/playlists) channel for more programming tutorials.