- 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 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.
- 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
activitiesto run code. The code that is being run is specified to run in certain stages of the
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
lifecyclethey 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
backstackwhich operates on the traditional stack logic of first in, last out. It is thanks to this
backstackthat 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).