SlideShare a Scribd company logo
1 of 19
Download to read offline
Android Application Development
Lecture 3: Getting Active Through
Activities and Intents
1. Android App Anatomy
2. What is Activity
3. Activity Lifecycle
4. Handling Runtime Changes
5. Multiple Activities
7. Activity Stack
8. Intents
Ahsanul Karim
karim.ahsanul@gmail.com
http://droidtraining.wordpress.com
Getting Active Through Activities Today We'll Discuss...
1. Android App Anatomy

4. Handling Runtime Changes

2. What is Activity

5. Multiple Activities

3. Activity Lifecycle

6. Activity Stack
Getting Active Through Activities

Application Anatomy

There are 4 types of application components/building blocks:
Activities
1. Activity provides user interface
2. Usually represents a single screen
3. Can contain one or more views
4. Extends the Activity base class
BroadcastReceiver
1. Receives and Reacts to broadcast
Intents
2. No UI but can start an Activity
3. Extends the BroadcastReceiver
base class

Services
1. No user interface
2. Runs in background
3. Extends the Service base class

ContentProvider
s
1. Makes application data available
to other apps [data sharing]
2. Uses SQLite database as storage
3. Extends the ContentProvider base
class
Getting Active Through Activities

What is Activity...

Activity
Activity provides a user generally with an interactive screen to do something like:
Dialing the phone, View a map or list of something etc.
1. An application usually consists of multiple activities.
2. Typically, one activity in an application is specified as the "main" activity.

The <action> element specifies that this is the "main" entry point to the application.
The <category> element specifies that this activity should be listed in the system's
application launcher (to allow users to launch this activity).
Activity Lifecycle
Getting Active Through Activities

Activity Lifecycle [1]

During the life of an activity, the system calls a core set of lifecycle methods

We'll see a demo on this...
Getting Active Through Activities

Activity Lifecycle [2]

Depending on the complexity of the activity, we may not need to implement all
the lifecycle methods. Implementing activity lifecycle methods properly
ensures the app behaves well in several ways, including that it:
1. Does not crash if the user receives
a phone call or switches to another
app while using the app.

2. Does not consume valuable system
resources when the user is not actively
using it.

3. Does not lose the user's progress if
they leave the app and return to it
later.

4. Does not crash or lose the user's
progress when the screen rotates
between landscape and portrait
orientation.
Getting Active Through Activities

Activity Lifecycle [3]

Starting an Activity

three main callbacks that called in sequence when creating new instance of activity:
onCreate(), onStart(), and onResume(). After sequence of callbacks complete, activity
reaches the Resumed state where users can interact with the activity.
Getting Active Through Activities

Activity Lifecycle [5]

Pausing & Resuming an Activity

When a semi-transparent activity obscures your activity, the system calls
onPause() and the activity waits in the Paused state (1). If the user returns to the
activity while it's still paused, the system calls onResume()
Getting Active Through Activities

Activity Lifecycle [6]

Pausing: onPause()
We should usually use the onPause() callback to:
1. Stop animations or other ongoing actions that could consume CPU.
2. Commit unsaved changes, but only if users expect such changes to be
permanently saved when they leave (such as a draft email).
3. Release system resources, such as broadcast receivers, handles to sensors
(like GPS), or any resources that may affect battery life.

Operations in onPause() must be kept simple in order to allow for a speedy
transition to the user's next destination.
Getting Active Through Activities

Activity Lifecycle [7]

Stopping & Restarting an Activity

When the user leaves your activity, the system calls onStop() to stop the activity (1).
If the user returns while the activity is stopped, the system calls onRestart() (2),
quickly followed by onStart() (3) and onResume() (4).
Getting Active Through Activities

Activity Lifecycle [8]

Stopping & Restarting an Activity
There are a few of key scenarios in which activity is stopped and restarted:
1. User opens the Recent Apps
window and switches from the app to
another app. If the user returns to the
app from the Home screen launcher
icon or the Recent Apps window, the
activity restarts.

2. When a new Activity is launched.
The current activity is stopped when
the second activity is created. If the
user then presses the Back button, the
first activity is restarted.

3. The user receives a phone call while using your app on his or her phone.
We should use onStop() to perform larger, more CPU intensive shut-down
operations, such as writing information to a database.
Getting Active Through Activities

Activity Lifecycle [9]

Recreating an Activity

As the system begins to stop your activity, it calls onSaveInstanceState() (1) so you
can specify additional state data you'd like to save in case the Activity instance must
be recreated. If the activity is destroyed and the same instance must be recreated,
the system passes the state data defined at (1) to both the onCreate() method (2)
and the onRestoreInstanceState() method (3).
Getting Active Through Activities

Runtime Changes...

Handling Runtime Changes
Some device configurations can change during runtime (such as screen
orientation, keyboard availability, and language). When such a change occurs,
onDestroy()is called, followed by onCreate().
The restart behavior is designed to help the application adapt to new
configurations by automatically reloading the application with alternative
resources that match the new device configuration.
Often we have the following 2 options:
Retain an object at configuration change
Allow the activity to restart when a
configuration changes, but carry a
stateful Object to the new instance of
activity.

Handle the configuration change yourself
Prevent the system from restarting activity
but receive a callback when the config
changes.
.
Getting Active Through Activities

Multiple Activities..

1. Adding New Activity
2. Passing Data Between Activities Using Intents
3. Passing Objects Between Activities
4. Starting New Activity For Result
5. Starting Activities of Other Applications
Getting Active Through Activities

A representation of how each new
activity in a task adds an item to the
back stack. When the user presses
the Back button, the current activity is
destroyed and the previous activity
resumes.

Activity Stack [1]
Getting Active Through Activities

Activity Stack [2]

Default behavior for activities and tasks:
1. When Activity A starts Activity B, Activity A is stopped, but the system retains its
state (such as scroll position and text entered into forms). If the user presses the
Back button while in Activity B, Activity A resumes with its state restored.
2. When the user leaves a task by pressing the Home button, the current activity is
stopped and its task goes into the background. The system retains the state of every
activity in the task. If the user later resumes the task by selecting the launcher icon
that began the task, the task comes to the foreground and resumes the activity at the
top of the stack.
3. If the user presses the Back button, the current activity is popped from the stack
and destroyed. The previous activity in the stack is resumed. When an activity is
destroyed, the system does not retain the activity's state.
4. Activities can be instantiated multiple times, even from other tasks.
Getting Active Through Activities

Activity Stack [3]

Controlling Activity Stack:
<activity> attributes:
taskAffinity

Intent flags:

launchMode

FLAG_ACTIVITY_NEW_TASK

allowTaskReparenting

FLAG_ACTIVITY_CLEAR_TOP

clearTaskOnLaunch

FLAG_ACTIVITY_SINGLE_TOP

alwaysRetainTaskState
finishOnTaskLaunch
Getting Active Through Activities

Questions...

The art and science of asking questions is the
source of all knowledge.
-Thomas Berger

More Related Content

What's hot

Android Tutorials : Basic widgets
Android Tutorials : Basic widgetsAndroid Tutorials : Basic widgets
Android Tutorials : Basic widgetsPrajyot Mainkar
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAhsanul Karim
 
Android application-component
Android application-componentAndroid application-component
Android application-componentLy Haza
 
Sensors in Android (old)
Sensors in Android (old)Sensors in Android (old)
Sensors in Android (old)Ahsanul Karim
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsAhsanul Karim
 
Day: 2 Environment Setup for Android Application Development
Day: 2 Environment Setup for Android Application DevelopmentDay: 2 Environment Setup for Android Application Development
Day: 2 Environment Setup for Android Application DevelopmentAhsanul Karim
 
Marakana Android User Interface
Marakana Android User InterfaceMarakana Android User Interface
Marakana Android User InterfaceMarko Gargenta
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAhsanul Karim
 
Android appwidget
Android appwidgetAndroid appwidget
Android appwidgetKrazy Koder
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & viewsma-polimi
 
Londroid Android Home Screen Widgets
Londroid Android Home Screen WidgetsLondroid Android Home Screen Widgets
Londroid Android Home Screen WidgetsRichard Hyndman
 
android activity
android activityandroid activity
android activityDeepa Rani
 

What's hot (20)

Android Services
Android ServicesAndroid Services
Android Services
 
Android Tutorials : Basic widgets
Android Tutorials : Basic widgetsAndroid Tutorials : Basic widgets
Android Tutorials : Basic widgets
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
 
Android Lesson 2
Android Lesson 2Android Lesson 2
Android Lesson 2
 
Android application-component
Android application-componentAndroid application-component
Android application-component
 
Sensors in Android (old)
Sensors in Android (old)Sensors in Android (old)
Sensors in Android (old)
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
Day: 2 Environment Setup for Android Application Development
Day: 2 Environment Setup for Android Application DevelopmentDay: 2 Environment Setup for Android Application Development
Day: 2 Environment Setup for Android Application Development
 
Training android
Training androidTraining android
Training android
 
Marakana Android User Interface
Marakana Android User InterfaceMarakana Android User Interface
Marakana Android User Interface
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
Android components
Android componentsAndroid components
Android components
 
Android Widget
Android WidgetAndroid Widget
Android Widget
 
Android Basics
Android BasicsAndroid Basics
Android Basics
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form Widgets
 
Android appwidget
Android appwidgetAndroid appwidget
Android appwidget
 
Unit2
Unit2Unit2
Unit2
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
Londroid Android Home Screen Widgets
Londroid Android Home Screen WidgetsLondroid Android Home Screen Widgets
Londroid Android Home Screen Widgets
 
android activity
android activityandroid activity
android activity
 

Viewers also liked

Introduction to Android Development: Before Getting Started
Introduction to Android Development: Before Getting StartedIntroduction to Android Development: Before Getting Started
Introduction to Android Development: Before Getting StartedAhsanul Karim
 
Day1 before getting_started
Day1 before getting_startedDay1 before getting_started
Day1 before getting_startedAhsanul Karim
 
Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2Ahsanul Karim
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studioParinita03
 
Action Bar Sherlock tutorial
Action Bar Sherlock tutorialAction Bar Sherlock tutorial
Action Bar Sherlock tutorialAhsanul Karim
 
Day 2 android internals a quick overview
Day 2 android internals a quick overviewDay 2 android internals a quick overview
Day 2 android internals a quick overviewAhsanul Karim
 
Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)Ahsanul Karim
 
Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Ahsanul Karim
 
Android GPS Tutorial
Android GPS TutorialAndroid GPS Tutorial
Android GPS TutorialAhsanul Karim
 
Day 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location APIDay 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location APIAhsanul Karim
 
Ui layout (incomplete)
Ui layout (incomplete)Ui layout (incomplete)
Ui layout (incomplete)Ahsanul Karim
 

Viewers also liked (15)

AndroidManifest
AndroidManifestAndroidManifest
AndroidManifest
 
Introduction to Android Development: Before Getting Started
Introduction to Android Development: Before Getting StartedIntroduction to Android Development: Before Getting Started
Introduction to Android Development: Before Getting Started
 
GCM for Android
GCM for AndroidGCM for Android
GCM for Android
 
Mcq peresentation
Mcq  peresentationMcq  peresentation
Mcq peresentation
 
Day1 before getting_started
Day1 before getting_startedDay1 before getting_started
Day1 before getting_started
 
Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2Android Workshop Day 1 Part 2
Android Workshop Day 1 Part 2
 
List Views
List ViewsList Views
List Views
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studio
 
Action Bar Sherlock tutorial
Action Bar Sherlock tutorialAction Bar Sherlock tutorial
Action Bar Sherlock tutorial
 
Day 2 android internals a quick overview
Day 2 android internals a quick overviewDay 2 android internals a quick overview
Day 2 android internals a quick overview
 
Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)Day: 1 Introduction to Mobile Application Development (in Android)
Day: 1 Introduction to Mobile Application Development (in Android)
 
Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]Day 5: Android User Interface [View Widgets]
Day 5: Android User Interface [View Widgets]
 
Android GPS Tutorial
Android GPS TutorialAndroid GPS Tutorial
Android GPS Tutorial
 
Day 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location APIDay 9: Make Your App Location Aware using Location API
Day 9: Make Your App Location Aware using Location API
 
Ui layout (incomplete)
Ui layout (incomplete)Ui layout (incomplete)
Ui layout (incomplete)
 

Similar to Lecture 3 getting active through activities

Presentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestatePresentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestateOsahon Gino Ediagbonya
 
Android Activities.pdf
Android Activities.pdfAndroid Activities.pdf
Android Activities.pdfssusere71a07
 
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_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
 
App anatomy and life cycle
App anatomy and life cycleApp anatomy and life cycle
App anatomy and life cycleSV.CO
 
Android building blocks and application life cycle-chapter3
Android building blocks and application life cycle-chapter3Android building blocks and application life cycle-chapter3
Android building blocks and application life cycle-chapter3Dr. Ramkumar Lakshminarayanan
 
Anatomy of android application
Anatomy of android applicationAnatomy of android application
Anatomy of android applicationNikunj Dhameliya
 
Android activity
Android activityAndroid activity
Android activityMohNage7
 
Mad textbook 63-116
Mad textbook 63-116Mad textbook 63-116
Mad textbook 63-116PrathishGM
 
Android development Training Programme Day 2
Android development Training Programme Day 2Android development Training Programme Day 2
Android development Training Programme Day 2DHIRAJ PRAVIN
 
Android application development
Android application developmentAndroid application development
Android application developmentMd. Mujahid Islam
 
Threads handlers and async task, widgets - day8
Threads   handlers and async task, widgets - day8Threads   handlers and async task, widgets - day8
Threads handlers and async task, widgets - day8Utkarsh Mankad
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intentDiego Grancini
 

Similar to Lecture 3 getting active through activities (20)

Presentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestatePresentation on Android application life cycle and saved instancestate
Presentation on Android application life cycle and saved instancestate
 
Android Activities.pdf
Android Activities.pdfAndroid Activities.pdf
Android Activities.pdf
 
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_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
AndroidAndroid
Android
 
App anatomy and life cycle
App anatomy and life cycleApp anatomy and life cycle
App anatomy and life cycle
 
Android building blocks and application life cycle-chapter3
Android building blocks and application life cycle-chapter3Android building blocks and application life cycle-chapter3
Android building blocks and application life cycle-chapter3
 
Anatomy of android application
Anatomy of android applicationAnatomy of android application
Anatomy of android application
 
Android activity
Android activityAndroid activity
Android activity
 
Activity & Shared Preference
Activity & Shared PreferenceActivity & Shared Preference
Activity & Shared Preference
 
Mad textbook 63-116
Mad textbook 63-116Mad textbook 63-116
Mad textbook 63-116
 
Android development Training Programme Day 2
Android development Training Programme Day 2Android development Training Programme Day 2
Android development Training Programme Day 2
 
Android application development
Android application developmentAndroid application development
Android application development
 
Threads handlers and async task, widgets - day8
Threads   handlers and async task, widgets - day8Threads   handlers and async task, widgets - day8
Threads handlers and async task, widgets - day8
 
Android App Development - 02 Activity and intent
Android App Development - 02 Activity and intentAndroid App Development - 02 Activity and intent
Android App Development - 02 Activity and intent
 
Google android Activity lifecycle
Google android Activity lifecycle Google android Activity lifecycle
Google android Activity lifecycle
 
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
 
Activity
ActivityActivity
Activity
 
Activity
ActivityActivity
Activity
 
Activity
ActivityActivity
Activity
 

More from Ahsanul Karim

Lecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & PreferencesLecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & PreferencesAhsanul Karim
 
Lecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick OverviewLecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick OverviewAhsanul Karim
 
Lecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting StartedLecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting StartedAhsanul Karim
 
লেকচার ১ (ক)- শুরুর আগে:
লেকচার ১ (ক)- শুরুর আগে:লেকচার ১ (ক)- শুরুর আগে:
লেকচার ১ (ক)- শুরুর আগে:Ahsanul Karim
 
Day 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIDay 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIAhsanul Karim
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsAhsanul Karim
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycleAhsanul Karim
 
Day 1 Android: Before Getting Started
Day 1 Android: Before Getting StartedDay 1 Android: Before Getting Started
Day 1 Android: Before Getting StartedAhsanul Karim
 
Mobile Banking in Bangladesh: An Incomplete Study
Mobile Banking in Bangladesh: An Incomplete StudyMobile Banking in Bangladesh: An Incomplete Study
Mobile Banking in Bangladesh: An Incomplete StudyAhsanul Karim
 
Android before getting started
Android before getting startedAndroid before getting started
Android before getting startedAhsanul Karim
 

More from Ahsanul Karim (11)

Lecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & PreferencesLecture 5: Storage: Saving Data Database, Files & Preferences
Lecture 5: Storage: Saving Data Database, Files & Preferences
 
Lecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick OverviewLecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick Overview
 
Lecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting StartedLecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting Started
 
লেকচার ১ (ক)- শুরুর আগে:
লেকচার ১ (ক)- শুরুর আগে:লেকচার ১ (ক)- শুরুর আগে:
লেকচার ১ (ক)- শুরুর আগে:
 
Day 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts APIDay 15: Content Provider: Using Contacts API
Day 15: Content Provider: Using Contacts API
 
Day 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViewsDay 8: Dealing with Lists and ListViews
Day 8: Dealing with Lists and ListViews
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycle
 
Day 1 Android: Before Getting Started
Day 1 Android: Before Getting StartedDay 1 Android: Before Getting Started
Day 1 Android: Before Getting Started
 
Mobile Banking in Bangladesh: An Incomplete Study
Mobile Banking in Bangladesh: An Incomplete StudyMobile Banking in Bangladesh: An Incomplete Study
Mobile Banking in Bangladesh: An Incomplete Study
 
Android before getting started
Android before getting startedAndroid before getting started
Android before getting started
 
Client-Server
Client-ServerClient-Server
Client-Server
 

Recently uploaded

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 

Recently uploaded (20)

"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 

Lecture 3 getting active through activities

  • 1. Android Application Development Lecture 3: Getting Active Through Activities and Intents 1. Android App Anatomy 2. What is Activity 3. Activity Lifecycle 4. Handling Runtime Changes 5. Multiple Activities 7. Activity Stack 8. Intents Ahsanul Karim karim.ahsanul@gmail.com http://droidtraining.wordpress.com
  • 2. Getting Active Through Activities Today We'll Discuss... 1. Android App Anatomy 4. Handling Runtime Changes 2. What is Activity 5. Multiple Activities 3. Activity Lifecycle 6. Activity Stack
  • 3. Getting Active Through Activities Application Anatomy There are 4 types of application components/building blocks: Activities 1. Activity provides user interface 2. Usually represents a single screen 3. Can contain one or more views 4. Extends the Activity base class BroadcastReceiver 1. Receives and Reacts to broadcast Intents 2. No UI but can start an Activity 3. Extends the BroadcastReceiver base class Services 1. No user interface 2. Runs in background 3. Extends the Service base class ContentProvider s 1. Makes application data available to other apps [data sharing] 2. Uses SQLite database as storage 3. Extends the ContentProvider base class
  • 4. Getting Active Through Activities What is Activity... Activity Activity provides a user generally with an interactive screen to do something like: Dialing the phone, View a map or list of something etc. 1. An application usually consists of multiple activities. 2. Typically, one activity in an application is specified as the "main" activity. The <action> element specifies that this is the "main" entry point to the application. The <category> element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).
  • 6. Getting Active Through Activities Activity Lifecycle [1] During the life of an activity, the system calls a core set of lifecycle methods We'll see a demo on this...
  • 7. Getting Active Through Activities Activity Lifecycle [2] Depending on the complexity of the activity, we may not need to implement all the lifecycle methods. Implementing activity lifecycle methods properly ensures the app behaves well in several ways, including that it: 1. Does not crash if the user receives a phone call or switches to another app while using the app. 2. Does not consume valuable system resources when the user is not actively using it. 3. Does not lose the user's progress if they leave the app and return to it later. 4. Does not crash or lose the user's progress when the screen rotates between landscape and portrait orientation.
  • 8. Getting Active Through Activities Activity Lifecycle [3] Starting an Activity three main callbacks that called in sequence when creating new instance of activity: onCreate(), onStart(), and onResume(). After sequence of callbacks complete, activity reaches the Resumed state where users can interact with the activity.
  • 9. Getting Active Through Activities Activity Lifecycle [5] Pausing & Resuming an Activity When a semi-transparent activity obscures your activity, the system calls onPause() and the activity waits in the Paused state (1). If the user returns to the activity while it's still paused, the system calls onResume()
  • 10. Getting Active Through Activities Activity Lifecycle [6] Pausing: onPause() We should usually use the onPause() callback to: 1. Stop animations or other ongoing actions that could consume CPU. 2. Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email). 3. Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life. Operations in onPause() must be kept simple in order to allow for a speedy transition to the user's next destination.
  • 11. Getting Active Through Activities Activity Lifecycle [7] Stopping & Restarting an Activity When the user leaves your activity, the system calls onStop() to stop the activity (1). If the user returns while the activity is stopped, the system calls onRestart() (2), quickly followed by onStart() (3) and onResume() (4).
  • 12. Getting Active Through Activities Activity Lifecycle [8] Stopping & Restarting an Activity There are a few of key scenarios in which activity is stopped and restarted: 1. User opens the Recent Apps window and switches from the app to another app. If the user returns to the app from the Home screen launcher icon or the Recent Apps window, the activity restarts. 2. When a new Activity is launched. The current activity is stopped when the second activity is created. If the user then presses the Back button, the first activity is restarted. 3. The user receives a phone call while using your app on his or her phone. We should use onStop() to perform larger, more CPU intensive shut-down operations, such as writing information to a database.
  • 13. Getting Active Through Activities Activity Lifecycle [9] Recreating an Activity As the system begins to stop your activity, it calls onSaveInstanceState() (1) so you can specify additional state data you'd like to save in case the Activity instance must be recreated. If the activity is destroyed and the same instance must be recreated, the system passes the state data defined at (1) to both the onCreate() method (2) and the onRestoreInstanceState() method (3).
  • 14. Getting Active Through Activities Runtime Changes... Handling Runtime Changes Some device configurations can change during runtime (such as screen orientation, keyboard availability, and language). When such a change occurs, onDestroy()is called, followed by onCreate(). The restart behavior is designed to help the application adapt to new configurations by automatically reloading the application with alternative resources that match the new device configuration. Often we have the following 2 options: Retain an object at configuration change Allow the activity to restart when a configuration changes, but carry a stateful Object to the new instance of activity. Handle the configuration change yourself Prevent the system from restarting activity but receive a callback when the config changes. .
  • 15. Getting Active Through Activities Multiple Activities.. 1. Adding New Activity 2. Passing Data Between Activities Using Intents 3. Passing Objects Between Activities 4. Starting New Activity For Result 5. Starting Activities of Other Applications
  • 16. Getting Active Through Activities A representation of how each new activity in a task adds an item to the back stack. When the user presses the Back button, the current activity is destroyed and the previous activity resumes. Activity Stack [1]
  • 17. Getting Active Through Activities Activity Stack [2] Default behavior for activities and tasks: 1. When Activity A starts Activity B, Activity A is stopped, but the system retains its state (such as scroll position and text entered into forms). If the user presses the Back button while in Activity B, Activity A resumes with its state restored. 2. When the user leaves a task by pressing the Home button, the current activity is stopped and its task goes into the background. The system retains the state of every activity in the task. If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack. 3. If the user presses the Back button, the current activity is popped from the stack and destroyed. The previous activity in the stack is resumed. When an activity is destroyed, the system does not retain the activity's state. 4. Activities can be instantiated multiple times, even from other tasks.
  • 18. Getting Active Through Activities Activity Stack [3] Controlling Activity Stack: <activity> attributes: taskAffinity Intent flags: launchMode FLAG_ACTIVITY_NEW_TASK allowTaskReparenting FLAG_ACTIVITY_CLEAR_TOP clearTaskOnLaunch FLAG_ACTIVITY_SINGLE_TOP alwaysRetainTaskState finishOnTaskLaunch
  • 19. Getting Active Through Activities Questions... The art and science of asking questions is the source of all knowledge. -Thomas Berger