CHAPTER 3 Activities and Intents Chapter objectives: • Explore an Activity’s Lifecycle • Learn about saving and restoring an Activity • Understand Intents and how they are used with multiple Activities • Become familiar with passing data between Activities • Implement applications that require basic animation Activity transitions • Study Scene transitions 3.1 Activity Lifecycle • All applications are composed of at least one Activity class • In most cases, applications will require the use of several activities • Activities in an application are often loosely connected to each other. Information can be passed from one activity to another, but they remain distinct and separate in every other way. • Every application has one activity class that serves as the main activity • Each time a new activity starts, the previous activity is paused and its status is preserved by the Android system. • Activities must be declared in the AndroidManifest.xml file in order to be accessible to the system • Activities are defined using the <activity> tag. • An <activity> must be added as a child to the <application> element • The activities in an application are implemented as a subclass of Activity • The Activity class is an important part of every application’s overall lifecycle • When an application is first loaded, its main activity, specified within the AndroidManifest, is immediately created • Once the main activity is started, it is given a window in which to draw its layout • Its layout is its associated user interface screen. • The Activities in an application are managed by an Activity stack • New activities are pushed onto the top of the stack and become the running activity • The Activity class defines the following seven callback methods, beginning with the creation of the Activity and ending with its destruction • • • • • • • onCreate() onStart() onResume() onPause() onStop() onRestart() onDestroy() • If a paused or stopped activity requires destruction due to a system need for more memory, the activity will need to be recreated again. This will be done by calling onCreate(). • onStart() is always to be called after onRestart() • onStop() is called when the activity is no longer visible to the user • onDestroy() performs final cleanup prior to an activity’s destruction 3.2 Starting, Saving, and Restoring an Activity • When an activity is paused or stopped, the state of the activity is retained • When the system destroys an activity in order to recover memory, the memory for that activity object is also destroyed • A Bundle is a container for the activity state information that can be saved • The following is an incomplete list of these methods: • • • • • • • • putChar() putString() putBoolean() putByte() putFloat() putLong() putShort() putParcelable() • Recover the saved state from the Bundle that the system passes to the activity • onCreate() and onRestoreInstanceState() callback methods receive the same Bundle object that contains the instance state information • Check whether the activity’s state (stored in the Bundle object) is null before you attempt to read it • If it is null, then the system is creating a new instance of the Activity class, instead of restoring a previous one that was destroyed • During runtime, the user may alter the screen orientation. • The system will automatically recreate the currently running activity by calling onDestroy(), and then immediately calling onCreate(). • Application adapts to new configurations by automatically reloading the application with alternative resources, such as adaptive layouts for different screen size and orientation 3.3 Multiple Activities and the Intent Class • Applications that are built with multiple activities need to utilize the Intent class • This class provides the framework for the navigation from one screen to another. • An Intent object is a message from one component to another component, either within the application or outside the application • Intents are designed to communicate messages between three application core components • Activities • Broadcast receivers • Service: 3.3.1 Explicit Intents • Explicit Intents use a specific name when starting a component • This name will be the full Java class name of the activity or service • The most common use of an explicit Intent is the launching of a target component with a known name within the currently running application. 3.3.2 Implicit Intents • Unlike an explicit Intent, an implicit Intent does not name a specific component • it declares a general action to perform, which allows a component from another app to handle it • When an implicit Intent is created, the system locates the appropriate target component by comparing the contents of the Intent to an Intent filter • Intent filters are declared in the manifest file of other apps located on a given device • When the Intent has found a match with the Intent filter, the system starts that component and delivers it to the Intent object. 3.4 Handling Keyboard Visibility • Android shows or hides the soft keyboard when input focus moves into or out of an editable text field • The system also makes decisions about how your UI and the text field appear above the keyboard • It is possible to specify how you want your layout to appear when the keyboard is visible • Android gives focus to the first EditText element in the layout launched by a running activity • It is also possible to request the focus of a View programmatically • requestFocus can be called to give focus to a specific View or to one of its descendants • This can be useful in cases when you want to ensure that the keyboard is visible. 3.5 Passing Data • The Android framework provides a simple and flexible approach to working with multiple activities • Android also offers an efficient model for passing information between various activities. • Data can be passed as a message object to an activity implemented within the application or an activity outside of the applications • When an Intent object is constructed, its action is specified • This represents the action we want the Intent to trigger • This action will also send data across process boundaries • It is possible to add extended data to a given Intent • This is done using the putExtra() method • putExtra() requires two parameters: a name for the data and a String data value • The data name must include a package prefix 3.6 Basic Transitions Between Activities • The quality of an application can depend on several characteristics, such as content, usability, and design features • Enhancements made to interaction design, through the use of animation, can make a fundamental difference in the usability of an application • Interaction plays a large role • User interfaces are not static designs, but rather engaging and dynamic design patterns • Custom transition animations are resources that can be built by methods in XML code • The method overridePendingTransition() allows custom-built transitions to be entering and exiting activities • overridePendingTransition() requires two arguments: an enter animation implementation and an exit animation implementation 3.7 Scene Transitions • KitKat, Android 4.4, has a transitions framework that supports the definition of scene transitions • Scenes are used specifically for transition animations. • Scenes can be created as a View hierarchy, much like a layout • A scene contains values of various properties in the View hierarchy • As scenes enter or exit a View hierarchy, they will be animated based on these properties http://developer.android.com/guide/topics/resources/animation-resource.html