Presentation is loading. Please wait.

Presentation is loading. Please wait.

Activities and Intents

Similar presentations


Presentation on theme: "Activities and Intents"— Presentation transcript:

1 Activities and Intents
CHAPTER 3 Activities and Intents 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

2 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

3 Activities must be declared in the AndroidManifest
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

4

5

6 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

7 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 When an activity is running, it takes user focus. The previous activity remains just below the running activity in the stack, LIFO. Each time a new activity starts, the previous activity is stopped and preserved on the stack.

8 Each activity goes through its own life cycle
The Activity class defines the following callback methods, beginning with the creation of the Activity and ending with its destruction: onCreate()  onStart()  onResume()  onPause()  onStop()  onRestart()  onDestroy() 

9 When an activity has been paused because a previous activity is resumed, the onPause() is called. A paused state is the first indication that the user is exiting the activity. A paused activity does not receive user input and cannot execute any code. onPause() is called as part of the activity lifecycle when an activity is going into the background, but it has not been destroyed. onPause() is most often used for saving persistent state the activity might be editing. It is useful for presenting an edit-in-place model to the user. onPause() is a good place to stop actions that consume a noticeable of CPU in order to close resources that require exclusive access such as camera. Once an activity has been paused and a new activity comes into focus, the onStop() method is called.

10 Android has a limited amount of memory.
In some situations, the system will require more memory while an application is running. In such situation, the system may kill procceses that are on pause. As a precaution, it is useful to design activities to save the current state using onSaveInstanceStat(Bundle) If an activity has not been destroyed, it can be restarted once it has been stopped or paused. By onRestart() and followed by onStart(). The paused activity can resume interactions using onResume() onDestroy() is called when a running activity exits and needs to be destroyed by the system.

11 If a paused or stopped activity requires destruction due to a system need for more memory, the activity must be recreated. The can be done by calling onCreate() onStart() should always be called after onRestart(). onRestart() is triggered when an activity has been stopped and then restarted when the user navigates back to the activity. OnStop() is called when the activity is no longer visible to the user. The next call backs in the sequence are usually onRestart() or onDestroy()

12 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

13

14 3.2 Starting, Saving, 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 To safe guard the important information about activity, the state can be preserved by calling onSaveInstanceState()

15 Bundle saves state information using name-value pairs
A Bundle is a container for the activity state information that can be saved Bundle saves state information using name-value pairs The following are these methods to put data into Bundle:  putChar() putString() putBoolean() putByte() putFloat() putLong() putShort() putParcelable()  

16 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 

17

18 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

19

20 Write and View Logs with Logcat
Logcat window displays system messages: Garbage collection occurs Messages you add in your app with Log class Messages displayed in real time To see the message, using View>Tool Windows >Logcat or click Logcat 

21

22 Write log messages The Log class allows you to create log messages that appear in logcat: Log.e(String, String) (error) Log.w(String, String) (warning) Log.i(String, String) (information) Log.d(String, String) (debug) Log.v(String, String) (verbose)

23 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  

24 Intents are designed to communicate messages between three application core components:
Activities Broadcast receivers Service

25 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

26 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

27

28 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

29 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

30 System can resize your layout to the available space

31 The user presses the Back button, or
Triggered by: The user presses the Back button, or The Activity.finish() method is called Managing state - onSaveInstanceState is not called (since the activity is finished, you don’t need to save state) - onCreate doesn’t have a Bundle when the app is reopened, because the activity was finished and the state doesn’t need to be restored. App is finished and restarted

32 Triggered by: The user presses the Home button The user switches to another app (via Overview menu, from a notification, accepting a call, etc.) Managing state When your activity enters the Stopped state, the system uses onSaveInstanceState to save the app state in case the system kills the app’s process later on. Assuming the process isn’t killed, the activity instance is kept resident in memory, retaining all state. When the activity comes back to the foreground, the activity recalls this information. You don’t need to re-initialize components that were created earlier. User navigates away

33 Triggered by: Configuration changes, like a rotation User resizes the window in multi-window mode Managing state Configuration changes like rotation or a window resize should let users continue exactly where they left off. The activity is completely destroyed, but the state is saved and restored for the new instance. The Bundle in onCreate and onRestoreInstanceState is the same. Configuration changes

34 Triggered by: Enabling Multi-window mode (API 24+) and losing the focus Another app partially covers the running app (a purchase dialog, a runtime permission dialog, a third-party login dialog…) An intent chooser appears, such as a share dialog App is paused by the system

35 3.5 Passing Data Android 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 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 Action can also send data across process boundaries

36 One can also add extended data to a given Intent
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

37 ACTION_SEND Common use is distribution of text content
Example, a user viewing a web page from Android built-in browser to share URL

38 Manifest file should be modified

39 ACTION_SEND_MULTIPLE
Send multiple elements

40 3.6 Basic Transitions Between Activities
The quality of an application can depend on content usability design features Better UIs are engaging and dynamic Animation can make a fundamental difference in the usability of an application

41 overridePendingTransition() requires two arguments:
Custom transition animations are resources that can be built in XML code overridePendingTransition() allows custom-built transitions to be entering and exiting activities overridePendingTransition() requires two arguments: an enter animation an exit animation

42 3.7 Scene Transitions KitKat has a transitions framework that supports the definition of scene transitions Scenes are used specifically for transition animations can be created as a View hierarchy (i.e., layout) Can be merged or transitioned into a defined View within an activity Scenes 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

43

44 Every transition object has two functions:
Transitions and Scene produce animation on specific properties (e.g., visibility) Transition holds information about animation running on its target during a Scene change Every transition object has two functions: To capture property value To play animation based on the captured property values. TransitionManager is used to coordinate Scene with Transition objects.

45


Download ppt "Activities and Intents"

Similar presentations


Ads by Google