Activities and Intents

Slides:



Advertisements
Similar presentations
Application Fundamentals Android Development. Announcements Posting in D2L Tutorials.
Advertisements

Programming with Android: Activities
Android 02: Activities David Meredith
All About Android Introduction to Android 1. Creating a New App “These aren’t the droids we’re looking for.” Obi-wan Kenobi 1. Bring up Eclipse. 2. Click.
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
The Activity Class 1.  One application component type  Provides a visual interface for a single screen  Typically supports one thing a user can do,
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Android Life Cycle CS328 Dick Steflik. Life Cycle The steps that an application goes through from starting to finishing Slightly different than normal.
More on User Interface Android Applications. Layouts Linear Layout Relative Layout Table Layout Grid View Tab Layout List View.
Android Application Development 2013 PClassic Chris Murphy 1.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
CS378 - Mobile Computing Anatomy of an Android App and the App Lifecycle.
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
CS5103 Software Engineering Lecture 08 Android Development II.
ANDROID UI – FRAGMENTS. Fragment  An activity is a container for views  When you have a larger screen device than a phone –like a tablet it can look.
Android Mobile computing for the rest of us.* *Prepare to be sued by Apple.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Android Info mostly based on Pro Android 3.  User Applications  Java Libraries – most of Java standard edition ◦ Activities/Services ◦ UI/Graphics/View.
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
Activities and Intents. Activities Activity is a window that contains the user interface of your application,typically an application has one or more.
Copyright© Jeffrey Jongko, Ateneo de Manila University Of Activities, Intents and Applications.
Overview of Android Application Development
SpotOn Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
CS378 - Mobile Computing Intents. Allow us to use applications and components that are part of Android System – start activities – start services – deliver.
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Android – Fragments L. Grewe.
DKU-MUST Mobile ICT Education Center 10. Activity and Intent.
Activities and Intents Chapter 3 1. Objectives Explore an activity’s lifecycle Learn about saving and restoring an activity Understand intents and how.
CHAPTER 4 Fragments ActionBar Menus. Explore how to build applications that use an ActionBar and Fragments Understand the Fragment lifecycle Learn to.
Services. What is a Service? A Service is not a separate process. A Service is not a thread. A Service itself is actually very simple, providing two main.
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
Editing a Twitter search. Viewing search results in a browser.
Cosc 4735 Nougat API 24+ additions.
Android Application -Architecture.
Mobile Application Development BSCS-7 Lecture # 2
Working in the Forms Developer Environment
Activity and Fragment.
Basic Activities and Intents
Instructor: Mazhar Hussain
Activities, Fragments, and Events
Mobile Application Development BSCS-7 Lecture # 6
MAD.
Activities and Intents
Android Activities An application can have one or more activities, where Each activity Represents a screen that an app present to its user Extends the.
Android 9: The Activity Lifecycle
Anatomy of an Android App and the App Lifecycle
Android Mobile Application Development
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
The Android Activity Lifecycle
ANDROID UI – FRAGMENTS UNIT II.
CS5103 Software Engineering
CIS 470 Mobile App Development
Anatomy of an Android App and the App Lifecycle
Android Topics Android Activity Lifecycle and Experiment Toast
Activities and Intents
Activities and Intents
HNDIT2417 Mobile Application Development
Android Programming Tutorial
Programming Mobile Applications with Android
CIS 470 Mobile App Development
Activities and Intents
Mobile Programming Dr. Mohsin Ali Memon.
SE4S701 Mobile Application Development
Mobile Programming Dr. Mohsin Ali Memon.
Activities and Fragments
Android Development Tools
Activities, Fragments, and Intents
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

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

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 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 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.

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() 

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.

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.

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()

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, 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()

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()  

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

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 

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)

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

System can resize your layout to the available space

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

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

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

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

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

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

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

Manifest file should be modified

ACTION_SEND_MULTIPLE Send multiple elements

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

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

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

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.