Android activities

The Android operating system is very focused on the concept of an activity. An activity is a task or unit of work that users can perform on their screen. For example, users would perform a phone activity to dial a number and carry out a second activity to interact with their address book to locate the number. Each Android application is a collection of one or more activities that users can launch and press the hardware's back key on their device to exit or cancel. The users' history is kept in the Android back stack, which you can manipulate from code in special cases. When a new activity starts, the previous one is paused and maintained in memory for later use, unless the operating system is running low on memory.

Activities are loosely coupled with each other; in some ways, you can think of them as having completely separate states from one another in memory. Static classes, properties, and fields will persist the life of the application, but the common practice is to pass a state in an Android bundle. This is useful when passing an identifier for an item displayed in a list to edit that item in a new activity.

Activities have the following lifecycle callback methods that you can override:

  • OnCreate: This is the first method called when your activity is created. Set up your views and perform other loading logic here. Most importantly, you will call SetContentView here to set up your activity's view.
  • OnResume: This is called when your activity's view is visible on the screen. It is called if your activity is displayed for the first time and when the user returns to it from another activity.
  • OnPause: This is called to notify that the user has left your activity. It can happen prior to navigating to a new activity within your app, locking the screen, or hitting the home button. Assume that the user may not return, so you need to save any changes the user made here.
  • OnStart: This occurs immediately before OnResume when the activity's view is about to be displayed on the screen. It occurs when an activity starts and when a user returns to it from another activity.
  • OnStop: This occurs immediately after OnPause when the activity's view is no longer displayed on the screen.
  • OnRestart: This method occurs when the user returns to your activity from a previous activity.
  • OnActivityResult: This method is used to communicate with other activities in other applications on Android. It is used in conjunction with StartActivityForResult; for example, you would use this to interact with the Facebook application to login a user.
  • OnDestroy: This is called when your activity is about to be freed from memory. Perform any additional clean-up that could help the operating system here, such as disposing any other heavyweight objects the activity was using.

A flowchart of the Android lifecycle is as follows:

Android activities

Unlike iOS, Android does not enforce any design patterns upon its developers. However, it is not possible to get away without understanding the Android activity lifecycle to some degree. Many concepts with activities are parallel to controllers on iOS, for example, OnStart is equivalent to ViewWillAppear and OnResume is equivalent to ViewDidAppear.

Other methods to note when working with activities are as follows:

  • StartActivity(Type type): This method starts a new activity within your application and passes no extra information to the activity.
  • StartActivity(Intent intent): This is an overload method to start a new activity with Intent. This gives you the ability to pass additional information to the new activity, and you can also launch activities in other applications.
  • StartActivityForResult: This method starts a new activity with the anticipation of receiving OnActivityResult when the activity's operation is completed.
  • Finish: This will close the current activity and invoke OnDestroy when it is completely closed and no longer displayed on the screen. Depending on what is currently on the back stack, the user will return to a previous activity or the home screen.
  • SetContentView: This method sets the primary view to be displayed for an activity. It should be called within the OnCreate method prior to the activity being displayed on the screen.
  • FindViewById: This is a method to locate the view displayed in your activity. It has a generic version to return a view of the appropriate type.

You can think of intent as an object that describes the transition from one activity to another. You can pass additional data through intents as well as modify how the activity is displayed and modify the user's navigation history.

In addition to activities, Android has the concept of a fragment. You can think of a fragment to be a miniature activity that is displayed inside a parent activity. Fragments are useful for reusing different pieces of a UI throughout your apps and can also help you implement split screen navigation on tablets.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.244.216