Android Activities


What is Activity?

Activity is the fundamental concept in Android development. It is an entry point which provides a surface to let users interact with the application. Every activity is independent of each other though they work together to form a cohesive user experience.

Activity creates a window on which you can place your UI which the user can interact to. These are just glue classes that provide an interface for letting apps communicate with the system and vice-versa. So the result is that the OS can destroy these components anytime based on different reasons. Activity are called as UI-based class.

Things Activity provides:
  • To let the system know to keep the process running which is hosting the activity (app in a whole).
  • Keeps track of previous processes which the user can return to.
  • Helps in handling process kill scenarios, so that previous state can be restored.
  • Helps apps to implement user flow between each other, and systems to coordinate this flow.

Things to consider while designing an Activity?

All the code required to keep the app functional should not be part of the single component and different components should be created to share those responsibilities. Activity can then delegate those responsibilities to these different components. i.e We should focus on achieving the principle of “Separation of Concern.

Activity should ONLY handle UI and OS interactions.

What should activity look like?
  • As lean as possible
  • With minimal dependency on other components
  • Loosely bound with other activities
  • How to create and declare an Activity?

Important points to note:
  • Create new activity and extend it using AppCompactActivity
  • Register your activity by declaring it in AndroidManifest file
  • Add intent-filters to the activity declared in AndroidManifest file (required if it is the launcher activity)




How does Activity System work together to create an Activity?
  • Android system uses the concept of Looper, Handler and MessageQueues to govern the different callbacks required to connect to the app process.
  • ActivityThread#main is the starting point
  • APPLICATION_INFO_CHANGED message is scheduled (via scheduleApplicationInfoChanged method)
  • Eventually, this message is handled (in handleMessage method), calls handleApplicationInfoChanged
  • RELAUNCH_ACTIVITY message is scheduled (via handleApplicationInfoChanged method)
  • Eventually, this message is handled (in handleMessage method), called handleRelaunchActivityLocally which initializes some configuration and executes ActivityRelaunchItem transaction.
  • handleRelaunchActivity is called, which will subsequently call handleLaunchActivity which prepares ground for new activity launch and finally calls performLaunchActivity
  • performLaunchActivity is the core implementation of activity launch. This sets all the required properties of activity like context, intent, theme, and the state (ON_CREATE). Then calls callActivityOnCreate.
  • callActivityOnCreate calls performCreate method on activity object which finally provides onCreate callback which we receive in our Activity class.
  • This is the point where Activity is CREATED :)
Resources
  • https://developer.android.com/guide/components/activities/intro-activities
  • https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/ActivityThread.java
  • https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/app/Activity.java

Comments

Popular posts from this blog

Fragment Lifecycle

AndroidManifest

Activity Lifecycle