SlideShare ist ein Scribd-Unternehmen logo
1 von 6
Chapter 3
Building Blocks and Application Life Cycle
By
Dr. Ramkumar Lakshminarayanan
Objectives
3.1. To understand the building blocks of Android Application
3.2. Explore the life cycle of android application
Introduction
Android applications define a program in terms of functionality of data. They perform tasks,
display information to the screen, and act upon data from a variety of sources. Developing
Android applications for mobile devices with limited resources requires a thorough
understanding of the application lifecycle. Android also uses its own terminology for these
application building blocks—terms such as Context, Activity and Intent. This chapter
familiarizes you with the most important components of Android applications.
Building Blocks
A few objects are defined in the Android SDK that every developer needs to be familiar with.
The most important ones are activities, intents, services, and content providers.
• Context: The context is the central command center for an Android application. All
application-specific functionality can be accessed through the context.
• Activity: An Android application is a collection of tasks, each of which is called an
Activity. Each Activity within an application has a unique task or purpose.
• Intent: The Android operating system uses an asynchronous messaging mechanism to
match task requests with the appropriate Activity. Each request is packaged as an Intent.
An intent is a mechanism for describing a specific action, such as “pick a photo,” “phone
home,” or “open the pod bay doors.” In Android, just about everything goes through
intents. For example, there is an intent for “send an email.” If your application needs to
send mail, you can invoke that intent. Or if you’re writing a new email application, you
can register an activity to handle that intent and replace the standard mail program. The
next time somebody tries to send an email, they’ll get the option to use your program
instead of the standard one.
• Service: Tasks that do not require user interaction can be encapsulated in a service. A
service is most useful when the operations are lengthy (offloading time-consuming
processing) or need to be done regularly (such as checking a server for new mail).
• Content Providers : A content provider is a set of data wrapped up in a custom API to
read and write it. This is the best way to share global data between applications. For
example, Google provides a content provider for contacts. All the information there—
names, addresses, phone numbers, and so forth—can be shared by any application that
wants to use it.
Using the Application Context
The application Context is the central location for all top-level application functionality.
The Context class can be used to manage application-specific configuration details as well
as application-wide operations and data. Use the Application Context to access settings and
resources shared across multiple Activity instances.
Retrieving the Application Context
You can retrieve the Context for the current process using the getApplicationContext() method,
like this:
Context context = getApplicationContext();
Using the Application Context
After you have retrieved a valid application Context, it can be used to access
application wide features and services. Accessing Other Application Functionality Using
Context The application Context provides access to a number of other top-level
application features. Here are a few more things you can do with the application Context:
• Launch Activity instances
• Retrieve assets packaged with the application
• Request a system service (for example, location service)
• Manage private application files, directories, and databases
 Inspect and enforce application permissions
Performing Application task with activities
The Android Activity class (android.app.Activity) is core to any Android
application. Much of the time, you define and implement an Activity class for each
screen in your application. For example, a simple application might have the
following five Activities, as shown in Figure 3.1
• A Startup or Splash screen: This activity serves as the primary entry
point to the application. It displays the application name and version
information and transitions to the Main menu after a short interval.
• A Main Menu screen: This activity acts as a switch to drive the user to
the core Activities of the application. Here the users must choose what they
want to do within the application.
• Order screen: This activity is where the order will be placed.
• Confirm Screen: This activity displays the confirmation of the order and place the
order.
• A Help/About screen: This activity display the information about the usage of
the application.
Figure 3.1 Activity for an application
Life Cycle of Android Activity
Android application are launched with a main() method, the Android system initiates
code in an Activity instance by invoking specific callback methods that correspond to specific
stages of its lifecycle. There is a sequence of callback methods that start up an activity and a
sequence of callback methods that tear down an activity.
During the life of an activity, the system calls a core set of lifecycle methods in a
sequence similar to a step pyramid. That is, each stage of the activity lifecycle is a separate step
on the pyramid. As the system creates a new activity instance, each callback method moves the
activity state one step toward the top. The top of the pyramid is the point at which the activity is
running in the foreground and the user can interact with it.
Startup / Splash
Activity
Main Menu
Activity
Order Activity Help / About
Activity
Confirm Order
Activity
As the user begins to leave the activity, the system calls other methods that move the
activity state back down the pyramid in order to dismantle the activity. In some cases, the
activity will move only part way down the pyramid and wait (such as when the user switches to
another app), from which point the activity can move back to the top (if the user returns to the
activity) and resume where the user left off.
Figure 3.2 Activity Lifecycle
Implementing the activity lifecycle methods properly ensures the app behaves well in
several ways, including that it:
 Does not crash if the user receives a phone call or switches to another app while using
your app.
 Does not consume valuable system resources when the user is not actively using it.
 Does not lose the user's progress if they leave your app and return to it at a later time.
 Does not crash or lose the user's progress when the screen rotates between landscape and
portrait orientation.
Activity can exist in one of only three states for an extended period of time they are
Resumed, Paused and Stopped.
Resumed
In this state, the activity is in the foreground and the user can interact with it. Also sometimes
referred to as the "running" state.
Paused
In this state, the activity is partially obscured by another activity—the other activity that's in
the foreground is semi-transparent or doesn't cover the entire screen. The paused activity does
not receive user input and cannot execute any code.
Stopped
In this state, the activity is completely hidden and not visible to the user; it is considered to
be in the background. While stopped, the activity instance and all its state information such as
member variables is retained, but it cannot execute any code
Created and Started are transient and the system quickly moves from them to the next state
by calling the next lifecycle callback method. That is, after the system calls onCreate(), it quickly
calls onStart(), which is quickly followed by onResume()
When the user selects the app icon from the Home screen, the system calls the onCreate()
method for the Activity in the app that was declared to be the "launcher" (or "main") activity.
This is the activity that serves as the main entry point to the app's user interface.
To use an activity as the main activity it is to be defined in the Android manifest file,
AndroidManifest.xml, which is at the root of your project directory.
The main activity for the app must be declared in the manifest with an <intent-filter> that
includes the MAIN action and LAUNCHER category. For example:
When a new Android project is created with the Android SDK tools, the default project
files include an Activity class that's declared in the manifest with this filter. If either the MAIN
action or LAUNCHER category are not declared for one of your activities, then your app icon
will not appear in the Home screen's list of apps.
Chapter Summary
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Android SDK’s building blocks are activities, intents, services, and content providers. When
application activity starts for the first time, it comes to the foreground of the system and receives
user focus. During this process, the Android system calls a series of lifecycle methods on the
activity in which the developer has to set up the user interface and other components. If the user
performs an action that starts another activity or switches to another app, the system calls another
set of lifecycle methods on the application activity as it moves into the background. The
understanding of Application Lifecycle is important for Android Application Development.

Weitere ähnliche Inhalte

Was ist angesagt?

android layouts
android layoutsandroid layouts
android layoutsDeepa Rani
 
Plant performance monitor
Plant performance monitorPlant performance monitor
Plant performance monitorsayedshiban
 
Chapter 2 lesson-1 adding the action bar
Chapter 2 lesson-1 adding the action barChapter 2 lesson-1 adding the action bar
Chapter 2 lesson-1 adding the action barKalluri Vinay Reddy
 
Android app development lesson 1
Android app development lesson 1Android app development lesson 1
Android app development lesson 1Kalluri Vinay Reddy
 
Chapter 2 lesson-2 styling the action bar
Chapter 2 lesson-2 styling the action barChapter 2 lesson-2 styling the action bar
Chapter 2 lesson-2 styling the action barKalluri Vinay Reddy
 
Point and Click App Building Workshop
Point and Click App Building WorkshopPoint and Click App Building Workshop
Point and Click App Building WorkshopSalesforce Developers
 
Android Training (Services)
Android Training (Services)Android Training (Services)
Android Training (Services)Khaled Anaqwa
 
Salesforce Lightning Components Workshop
Salesforce Lightning Components WorkshopSalesforce Lightning Components Workshop
Salesforce Lightning Components WorkshopChristophe Coenraets
 

Was ist angesagt? (9)

android layouts
android layoutsandroid layouts
android layouts
 
Plant performance monitor
Plant performance monitorPlant performance monitor
Plant performance monitor
 
Chapter 2 lesson-1 adding the action bar
Chapter 2 lesson-1 adding the action barChapter 2 lesson-1 adding the action bar
Chapter 2 lesson-1 adding the action bar
 
Android app development lesson 1
Android app development lesson 1Android app development lesson 1
Android app development lesson 1
 
Chapter 2 lesson-2 styling the action bar
Chapter 2 lesson-2 styling the action barChapter 2 lesson-2 styling the action bar
Chapter 2 lesson-2 styling the action bar
 
Point and Click App Building Workshop
Point and Click App Building WorkshopPoint and Click App Building Workshop
Point and Click App Building Workshop
 
Android Training (Services)
Android Training (Services)Android Training (Services)
Android Training (Services)
 
Lightning Components Workshop v2
Lightning Components Workshop v2Lightning Components Workshop v2
Lightning Components Workshop v2
 
Salesforce Lightning Components Workshop
Salesforce Lightning Components WorkshopSalesforce Lightning Components Workshop
Salesforce Lightning Components Workshop
 

Ähnlich wie Android building blocks and application life cycle-chapter3

android_mod_3.useful for bca students for their last sem
android_mod_3.useful for bca students for their last semandroid_mod_3.useful for bca students for their last sem
android_mod_3.useful for bca students for their last semaswinbiju1652
 
android activity
android activityandroid activity
android activityDeepa Rani
 
Android development Training Programme Day 2
Android development Training Programme Day 2Android development Training Programme Day 2
Android development Training Programme Day 2DHIRAJ PRAVIN
 
Data Transfer between activities and Database
Data Transfer between activities and Database Data Transfer between activities and Database
Data Transfer between activities and Database faiz324545
 
Unit 5 Activity and Activity Life Cycle.pptx
Unit 5 Activity and Activity Life Cycle.pptxUnit 5 Activity and Activity Life Cycle.pptx
Unit 5 Activity and Activity Life Cycle.pptxShantanuDharekar
 
Android application development
Android application developmentAndroid application development
Android application developmentMd. Mujahid Islam
 
Android lifecycle
Android lifecycleAndroid lifecycle
Android lifecycleKumar
 
Android Activities.pdf
Android Activities.pdfAndroid Activities.pdf
Android Activities.pdfssusere71a07
 
Android Component.pptx
Android Component.pptxAndroid Component.pptx
Android Component.pptxQwerty140857
 
Guidelines for Android application design.pptx
Guidelines for Android application design.pptxGuidelines for Android application design.pptx
Guidelines for Android application design.pptxdebasish duarah
 
Android app fundamentals
Android app fundamentalsAndroid app fundamentals
Android app fundamentalsAmr Salman
 
Android apps development
Android apps developmentAndroid apps development
Android apps developmentMonir Zzaman
 
Kotlin for Android App Development Presentation
Kotlin for Android App Development PresentationKotlin for Android App Development Presentation
Kotlin for Android App Development PresentationKnoldus Inc.
 
Android application model
Android application modelAndroid application model
Android application modelmagicshui
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptxMugiiiReee
 
Mobile Application Development -Lecture 09 & 10.pdf
Mobile Application Development -Lecture 09 & 10.pdfMobile Application Development -Lecture 09 & 10.pdf
Mobile Application Development -Lecture 09 & 10.pdfAbdullahMunir32
 
Mobile Application Guideline | Mobile App Development Company
Mobile Application Guideline | Mobile App Development Company Mobile Application Guideline | Mobile App Development Company
Mobile Application Guideline | Mobile App Development Company Arna Softech Private Limited
 
Anatomy of android application
Anatomy of android applicationAnatomy of android application
Anatomy of android applicationNikunj Dhameliya
 

Ähnlich wie Android building blocks and application life cycle-chapter3 (20)

android_mod_3.useful for bca students for their last sem
android_mod_3.useful for bca students for their last semandroid_mod_3.useful for bca students for their last sem
android_mod_3.useful for bca students for their last sem
 
android activity
android activityandroid activity
android activity
 
Android development Training Programme Day 2
Android development Training Programme Day 2Android development Training Programme Day 2
Android development Training Programme Day 2
 
Data Transfer between activities and Database
Data Transfer between activities and Database Data Transfer between activities and Database
Data Transfer between activities and Database
 
Unit 5 Activity and Activity Life Cycle.pptx
Unit 5 Activity and Activity Life Cycle.pptxUnit 5 Activity and Activity Life Cycle.pptx
Unit 5 Activity and Activity Life Cycle.pptx
 
Android application development
Android application developmentAndroid application development
Android application development
 
Android lifecycle
Android lifecycleAndroid lifecycle
Android lifecycle
 
Android Activities.pdf
Android Activities.pdfAndroid Activities.pdf
Android Activities.pdf
 
Android Component.pptx
Android Component.pptxAndroid Component.pptx
Android Component.pptx
 
Guidelines for Android application design.pptx
Guidelines for Android application design.pptxGuidelines for Android application design.pptx
Guidelines for Android application design.pptx
 
Android app fundamentals
Android app fundamentalsAndroid app fundamentals
Android app fundamentals
 
Android apps development
Android apps developmentAndroid apps development
Android apps development
 
Android Development Basics
Android Development BasicsAndroid Development Basics
Android Development Basics
 
Android development session 2 - intent and activity
Android development   session 2 - intent and activityAndroid development   session 2 - intent and activity
Android development session 2 - intent and activity
 
Kotlin for Android App Development Presentation
Kotlin for Android App Development PresentationKotlin for Android App Development Presentation
Kotlin for Android App Development Presentation
 
Android application model
Android application modelAndroid application model
Android application model
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
 
Mobile Application Development -Lecture 09 & 10.pdf
Mobile Application Development -Lecture 09 & 10.pdfMobile Application Development -Lecture 09 & 10.pdf
Mobile Application Development -Lecture 09 & 10.pdf
 
Mobile Application Guideline | Mobile App Development Company
Mobile Application Guideline | Mobile App Development Company Mobile Application Guideline | Mobile App Development Company
Mobile Application Guideline | Mobile App Development Company
 
Anatomy of android application
Anatomy of android applicationAnatomy of android application
Anatomy of android application
 

Mehr von Dr. Ramkumar Lakshminarayanan

Mehr von Dr. Ramkumar Lakshminarayanan (20)

IT security awareness
IT security awarenessIT security awareness
IT security awareness
 
Basics of IT security
Basics of IT securityBasics of IT security
Basics of IT security
 
IT Security Awareness Posters
IT Security Awareness PostersIT Security Awareness Posters
IT Security Awareness Posters
 
Normalisation revision
Normalisation revisionNormalisation revision
Normalisation revision
 
Windows mobile programming
Windows mobile programmingWindows mobile programming
Windows mobile programming
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Web technology today
Web technology todayWeb technology today
Web technology today
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Phonegap for Android
Phonegap for AndroidPhonegap for Android
Phonegap for Android
 
Create and Sell Android App (in tamil)
Create and Sell Android App (in tamil)Create and Sell Android App (in tamil)
Create and Sell Android App (in tamil)
 
Android app - Creating Live Wallpaper (tamil)
Android app - Creating Live Wallpaper (tamil)Android app - Creating Live Wallpaper (tamil)
Android app - Creating Live Wallpaper (tamil)
 
Android Tips (Tamil)
Android Tips (Tamil)Android Tips (Tamil)
Android Tips (Tamil)
 
Android Animation (in tamil)
Android Animation (in tamil)Android Animation (in tamil)
Android Animation (in tamil)
 
Creating List in Android App (in tamil)
Creating List in Android App (in tamil)Creating List in Android App (in tamil)
Creating List in Android App (in tamil)
 
Single Touch event view in Android (in tamil)
Single Touch event view in Android (in tamil)Single Touch event view in Android (in tamil)
Single Touch event view in Android (in tamil)
 
Android Application using seekbar (in tamil)
Android Application using seekbar (in tamil)Android Application using seekbar (in tamil)
Android Application using seekbar (in tamil)
 
Rating Bar in Android Example
Rating Bar in Android ExampleRating Bar in Android Example
Rating Bar in Android Example
 
Creating Image Gallery - Android app (in tamil)
Creating Image Gallery - Android app (in tamil)Creating Image Gallery - Android app (in tamil)
Creating Image Gallery - Android app (in tamil)
 
Create Android App using web view (in tamil)
Create Android App using web view (in tamil)Create Android App using web view (in tamil)
Create Android App using web view (in tamil)
 
Hardware Interface in Android (in tamil)
Hardware Interface in Android (in tamil)Hardware Interface in Android (in tamil)
Hardware Interface in Android (in tamil)
 

Android building blocks and application life cycle-chapter3

  • 1. Chapter 3 Building Blocks and Application Life Cycle By Dr. Ramkumar Lakshminarayanan Objectives 3.1. To understand the building blocks of Android Application 3.2. Explore the life cycle of android application Introduction Android applications define a program in terms of functionality of data. They perform tasks, display information to the screen, and act upon data from a variety of sources. Developing Android applications for mobile devices with limited resources requires a thorough understanding of the application lifecycle. Android also uses its own terminology for these application building blocks—terms such as Context, Activity and Intent. This chapter familiarizes you with the most important components of Android applications. Building Blocks A few objects are defined in the Android SDK that every developer needs to be familiar with. The most important ones are activities, intents, services, and content providers. • Context: The context is the central command center for an Android application. All application-specific functionality can be accessed through the context. • Activity: An Android application is a collection of tasks, each of which is called an Activity. Each Activity within an application has a unique task or purpose. • Intent: The Android operating system uses an asynchronous messaging mechanism to match task requests with the appropriate Activity. Each request is packaged as an Intent. An intent is a mechanism for describing a specific action, such as “pick a photo,” “phone home,” or “open the pod bay doors.” In Android, just about everything goes through intents. For example, there is an intent for “send an email.” If your application needs to send mail, you can invoke that intent. Or if you’re writing a new email application, you can register an activity to handle that intent and replace the standard mail program. The next time somebody tries to send an email, they’ll get the option to use your program instead of the standard one. • Service: Tasks that do not require user interaction can be encapsulated in a service. A
  • 2. service is most useful when the operations are lengthy (offloading time-consuming processing) or need to be done regularly (such as checking a server for new mail). • Content Providers : A content provider is a set of data wrapped up in a custom API to read and write it. This is the best way to share global data between applications. For example, Google provides a content provider for contacts. All the information there— names, addresses, phone numbers, and so forth—can be shared by any application that wants to use it. Using the Application Context The application Context is the central location for all top-level application functionality. The Context class can be used to manage application-specific configuration details as well as application-wide operations and data. Use the Application Context to access settings and resources shared across multiple Activity instances. Retrieving the Application Context You can retrieve the Context for the current process using the getApplicationContext() method, like this: Context context = getApplicationContext(); Using the Application Context After you have retrieved a valid application Context, it can be used to access application wide features and services. Accessing Other Application Functionality Using Context The application Context provides access to a number of other top-level application features. Here are a few more things you can do with the application Context: • Launch Activity instances • Retrieve assets packaged with the application • Request a system service (for example, location service) • Manage private application files, directories, and databases  Inspect and enforce application permissions Performing Application task with activities The Android Activity class (android.app.Activity) is core to any Android application. Much of the time, you define and implement an Activity class for each screen in your application. For example, a simple application might have the following five Activities, as shown in Figure 3.1
  • 3. • A Startup or Splash screen: This activity serves as the primary entry point to the application. It displays the application name and version information and transitions to the Main menu after a short interval. • A Main Menu screen: This activity acts as a switch to drive the user to the core Activities of the application. Here the users must choose what they want to do within the application. • Order screen: This activity is where the order will be placed. • Confirm Screen: This activity displays the confirmation of the order and place the order. • A Help/About screen: This activity display the information about the usage of the application. Figure 3.1 Activity for an application Life Cycle of Android Activity Android application are launched with a main() method, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to specific stages of its lifecycle. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity. During the life of an activity, the system calls a core set of lifecycle methods in a sequence similar to a step pyramid. That is, each stage of the activity lifecycle is a separate step on the pyramid. As the system creates a new activity instance, each callback method moves the activity state one step toward the top. The top of the pyramid is the point at which the activity is running in the foreground and the user can interact with it. Startup / Splash Activity Main Menu Activity Order Activity Help / About Activity Confirm Order Activity
  • 4. As the user begins to leave the activity, the system calls other methods that move the activity state back down the pyramid in order to dismantle the activity. In some cases, the activity will move only part way down the pyramid and wait (such as when the user switches to another app), from which point the activity can move back to the top (if the user returns to the activity) and resume where the user left off. Figure 3.2 Activity Lifecycle Implementing the activity lifecycle methods properly ensures the app behaves well in several ways, including that it:  Does not crash if the user receives a phone call or switches to another app while using your app.  Does not consume valuable system resources when the user is not actively using it.  Does not lose the user's progress if they leave your app and return to it at a later time.  Does not crash or lose the user's progress when the screen rotates between landscape and portrait orientation. Activity can exist in one of only three states for an extended period of time they are Resumed, Paused and Stopped. Resumed In this state, the activity is in the foreground and the user can interact with it. Also sometimes referred to as the "running" state.
  • 5. Paused In this state, the activity is partially obscured by another activity—the other activity that's in the foreground is semi-transparent or doesn't cover the entire screen. The paused activity does not receive user input and cannot execute any code. Stopped In this state, the activity is completely hidden and not visible to the user; it is considered to be in the background. While stopped, the activity instance and all its state information such as member variables is retained, but it cannot execute any code Created and Started are transient and the system quickly moves from them to the next state by calling the next lifecycle callback method. That is, after the system calls onCreate(), it quickly calls onStart(), which is quickly followed by onResume() When the user selects the app icon from the Home screen, the system calls the onCreate() method for the Activity in the app that was declared to be the "launcher" (or "main") activity. This is the activity that serves as the main entry point to the app's user interface. To use an activity as the main activity it is to be defined in the Android manifest file, AndroidManifest.xml, which is at the root of your project directory. The main activity for the app must be declared in the manifest with an <intent-filter> that includes the MAIN action and LAUNCHER category. For example: When a new Android project is created with the Android SDK tools, the default project files include an Activity class that's declared in the manifest with this filter. If either the MAIN action or LAUNCHER category are not declared for one of your activities, then your app icon will not appear in the Home screen's list of apps. Chapter Summary <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
  • 6. Android SDK’s building blocks are activities, intents, services, and content providers. When application activity starts for the first time, it comes to the foreground of the system and receives user focus. During this process, the Android system calls a series of lifecycle methods on the activity in which the developer has to set up the user interface and other components. If the user performs an action that starts another activity or switches to another app, the system calls another set of lifecycle methods on the application activity as it moves into the background. The understanding of Application Lifecycle is important for Android Application Development.