Chapter - 1 (Mad)
Chapter - 1 (Mad)
Chapter - 1 (Mad)
COMPUTING
07/28/24 2
Importance
07/28/24
Topic Objectives
07/28/24 4
What is Android ?
• It is an open source.
5
History of Android
6
Open Handset Alliance
7
Features of Android
• It is an Open Source.
• Anyone can customize the Android Platform.
• There are lot of applications that can be chosen by the
customer.
• Application Framework enabling reuse and
replacement of component
• Dalvik Virtual Machine optimized for mobile devices.
• Media support for common audio, video and still image
formats(MPEG4,MP3,AAC,AMR,JPG,PNG,GIF)
• Bluetooth,EDGE,3G,WiFi (hardware dependent)
8
Features of Android
10
Android Architecture
11
DVM
12
Requirements for Android
• Download Android Studio
• From http://developer.android.com/sdk/installing/index.html
• System Requirements for Windows
• Microsoft® Windows® 8/7/Vista (32 or 64-bit)
• 2 GB RAM minimum, 8 GB RAM recommended
• 400 MB hard disk space
• At least 1 GB for Android SDK, emulator system images, and caches
• 1280 x 800 minimum screen resolution
• Kotlin Development Kit (JDK) 7
• Optional for accelerated emulator: Intel® processor with support for Intel®
VT-x, Intel® EM64T (Intel® 64), and Execute Disable (XD) Bit
functionality
13
Installation
Kotlin
•Visit http://www.oracle.com/technetwork/Kotlin/Kotlinse/downloads/index.html
•Install it and Set Path [Select Start menu > Computer > System Properties >
Advanced System Properties. Then open Advanced tab > Environment Variables
and add a new system variable Kotlin_HOME that points to your JDK folder, for
example C:\Program Files\Kotlin\jdk1.7.0_4]
Android Studio
•Visit http://developer.android.com/sdk/index.html and download Download
Android Studio.
•Run executable file of setup.
•Follow the setup wizard to install Android Studio and any necessary SDK tools.
14
Manifest
• Every Android app must include an
AndroidManifest.xml file describing functionality
15
What is an Activity?
● An Activity is an application component
● Represents one window, one hierarchy of views
● Typically fills the screen, but can be embedded in other Activity or a
appear as floating window
● Kotlin class, typically one Activity in one file
16
What does an Activity do?
● Represents an activity, such as ordering groceries, sending email, or
getting directions
● Handles user interactions, such as button clicks, text entry, or login
verification
● Can start other activities in the same or other apps
● Has a life cycle—is created, started, runs, is paused, resumed, stopped,
and destroyed
17
Examples of activities
18
Apps and activities
● Activities are loosely tied together to make up an app
● First Activity user sees is typically called "main activity"
● Activities can be organized in parent-child relationships in the Android
manifest to aid navigation
19
Layouts and Activities
20
Activity lifecycle
21
Activity Life Cycle
Method Description
onCreate called when activity is first created.
called when activity is becoming
onStart
visible to the user.
called when activity will start
onResume
interacting with the user.
onPause called when activity is not visible to
called when activity is no longer
onStop
visible to the user.
called after your activity is stopped,
onRestart
prior to start.
onDestroy called before the activity is destroyed.
22
What is the Activity Lifecycle?
More formally:
●A directed graph of all the states an Activity can be in, and the
callbacks associated with transitioning from each state to the next
one
23
What is the Activity Lifecycle?
24
Activity states and app visibility
● Created (not visible yet)
● Started (visible)
● Resume (visible)
● Paused(partially invisible)
● Stopped (hidden)
● Destroyed (gone from memory)
26
Callbacks and when they are called
onCreate(Bundle savedInstanceState)—static initialization
onStart()—when Activity (screen) is becoming visible
onRestart()—called if Activity was stopped (calls onStart())
onResume()—start to interact with user
onPause()—about to resume PREVIOUS Activity
onStop()—no longer visible, but still exists and all state info preserved
onDestroy()—final call before Android system destroys Activity
27
Activity states and callbacks graph
28
Implementing and overriding callbacks
29
onCreate() –> Created
● Called when the Activity is first created, for example when user taps
launcher icon
● Does all static setup: create views, bind data to lists, ...
● Only called once during an activity's lifetime
● Takes a Bundle with Activity's previously frozen state, if there was one
● Created state is always followed by onStart()
30
onCreate(Bundle savedInstanceState)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
}
31
onStart() –> Started
32
onStart()
@Override
protected void onStart() {
super.onStart();
// The activity is about to become visible.
}
33
onRestart() –> Started
34
onRestart()
@Override
protected void onRestart() {
super.onRestart();
// The activity is between stopped and started.
}
35
onResume() –> Resumed/Running
36
onResume()
@Override
protected void onResume() {
super.onResume();
// The activity has become visible
// it is now "resumed"
}
37
onPause() –> Paused
● Called when system is about to resume a previous Activity
● The Activity is partly visible but user is leaving the Activity
● Typically used to commit unsaved changes to persistent data, stop
animations and anything that consumes resources
● Implementations must be fast because the next Activity is not resumed until
this method returns
● Followed by either onResume() if the Activity returns back to the front, or
onStop() if it becomes invisible to the user
38
onPause()
@Override
protected void onPause() {
super.onPause();
// Another activity is taking focus
// this activity is about to be "paused"
}
39
onStop() –> Stopped
● Called when the Activity is no longer visible to the user
● New Activity is being started, an existing one is brought in front
of this one, or this one is being destroyed
● Operations that were too heavy-weight for onPause()
● Followed by either onRestart() if Activity is coming back to
interact with user, or onDestroy() if Activity is going away
40
onStop()
@Override
protected void onStop() {
super.onStop();
// The activity is no longer visible
// it is now "stopped"
}
41
onDestroy() –> Destroyed
● Final call before Activity is destroyed
● User navigates back to previous Activity, or configuration changes
● Activity is finishing or system is destroying it to save space
● Call isFinishing() method to check
● System may destroy Activity without calling this, so use
onPause() or onStop() to save data or state
42
onDestroy()
@Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
}
43
Activity instance
state
44
When does config change?
45
What happens on config change?
46
Activity instance state
● State information is created while the Activity is running, such
as a counter, user text, animation progression
47
Saving and
restoring
Activity state
48
What the system saves
●System saves only:
○ State of views with unique ID (android:id) such as text
entered into EditText
○ Intent that started activity and data in its extras
●You are responsible for saving other activity and user progress
data
49
Saving instance state
50
onSaveInstanceState(Bundle outState)
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Add information for saving HelloToast counter
// to the to the outState bundle
outState.putString("count",
String.valueOf(mShowCount.getText()));
}
51
Restoring instance state
52
Restoring in onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShowCount = findViewById(R.id.show_count);
if (savedInstanceState != null) {
String count = savedInstanceState.getString("count");
if (mShowCount != null)
mShowCount.setText(count);
}
}
53
onRestoreInstanceState(Bundle state)
@Override
public void onRestoreInstanceState (Bundle mySavedState) {
super.onRestoreInstanceState(mySavedState);
if (mySavedState != null) {
String count = mySavedState.getString("count");
if (count != null)
mShowCount.setText(count);
}
}
54
Instance state and app restart
When you stop and restart a new app session, the Activity instance
states are lost and your activities will revert to their default
appearance
If you need to save user data between app sessions, use shared
preferences or a database.
55
BIBLIOGRAPHY
Text Books-
•Android Programming for Beginners by John Horton
•Reto Meier, “Profesional Android 4 Aplication Development” ,
John Wiley& Sons, Inc, 2012.
Reference Material-
•Zigurd Mednieks, Laird Dornin, Blake Meike G, and Masumi
Nakamura, “Programming Android”, O’Reily boks
07/28/24 56
Thank You
07/28/24 57