Android - Activity
Objective #1: What is an activity?
- Activities are used in virtually every Android application. Usually, an activity is a single screen in an Android app. An activity in Android is similar to a form in Visual Basic.
- Each activity is represented as a Java source code class. The name of the Java class is the same as the name of the file (except for the .java file extension).
- Usually an activity's class file makes use of an XML file that defines and lays out the activity's interface that is viewed by the user. In this way, the interface of an activity is separated from the programming and logic because it is found in a different file.
- The XML file usually does not have the same as the Java source code file. For example, a class file named Level1.java could use an XML file named mainactivity.xml as its interface layout.
- XML layout files for all the activities in a project are saved in a subfolder named layout which is a subfolder of the project's res folder.
- Rather than hiding and showing views (Buttons, TextViews, etc.), it is more efficient to switching activities when displaying different screens to the user.
- Activities have four “states”:
- Activities in the running state are what is on the foreground and is seen.
- Activities are in the paused state when an activity is still visible but has lost focus. When an activity is in this state, it is still considered “active” but it can be killed by low memory.
- Activities are considered in the stopped state when a new activity is created. Unlike an activity in the paused state, activities in the stopped stage are invisible BUT it still retains its information.
- Activities are killed when either the user or the system ends the activity. In order for the activity to be reused, the user must restart it.
- This visual is a good way to understand the code and which stage an activity is in: http://developer.android.com/images/activity_lifecycle.png
- Activities have three different loops that limit an activities lifetime:
- The entire lifetime is an activity that will not be killed until the onCreate method is used and ends with the onDestroy method. An entire lifetime loop will execute all of the code inside of the onCreate method, this is what is primarily used.
- The visible lifetime is a loop that will begin with the onStart method and ends with the onStop( method. The activity is visible in this stage; however, it may not be in the foreground. These methods can be called more than one time in a single activity.
- The foreground lifetime is the lifetime loop that occurs within the onResume and onStop methods. The onResume method brings this activity to the top of the activity stack and is brought to the front in the same state it was paused in. This is useful for going in between screens, maybe for menus and game projects.
- Android tries to keep the activities running for as long as possible. Although this may not be possible due to memory restrictions or method executions, Android activities have been programmed with an order of importance for activity “killable stages.” The highest level stage is almost never killed, while the lowest is always killed. These are the stages in order of importance:
- The foreground activity is the visible and foremost activity being used by the user. An activity in this stage will only be killed if there is an extreme memory drain in the system that memory needs to be conserved.
- The visible activity is the visible to the user but is not the foremost activity. This stage is very important, just as the foreground activity is, and will only be killed if its sacrifice will be beneficial to the success of the foreground activity.
- The background activity stage is when an activity has been paused and is no longer visible; therefore, no longer critical to the success of the application. The system may assume killing this activity may be useful to safely conserve memory for the foreground and visible activity stages.
- The lowest stage is the empty process stage. This stage is killed very quickly by the system because it is in a stage in which nothing is being used or hosted. The usual classes in an empty process stage are the Service and BroadcastReceiver classes. These two classes are killed quickly because anything executed outside of an activity must be executed somewhere in the Service or BroadcastReceiver class.
Objective #2: What is an activity stack?
- Activities are arranged in what is known as an activity stack. Activity stacks are the arrangement of ongoing and new activities.
- An activity stack is the list of activities that have occurred. It is a top-loading list, in which the top most activity is the current one running. The previous activities are in the paused state and will not return to the running state until the top most activity is killed.
- Activity stacks only readily contain two activities, the current activity and one prior. After both activities are killed, the system returns to the home page. However, the “Recent Tasks” page contains all of the earlier activities.
- When the user presses the back button on the Android phone, the system will return to the previous activity while pausing the one that was just running.
- Activities are the only things contained in an activity stack.