University Institute of Computing: Master of Computer Application Mobile Application Development
University Institute of Computing: Master of Computer Application Mobile Application Development
COMPUTING
Master of Computer Application
Mobile Application Development.
11/05/22
Topic Objectives
• To write and run a Kotlin programs.
• To work with different control flow statements.
• To implement programs based on Object Oriented
Programming.
• To understand the need of getter and setters.
• To work with inheritance based programs.
• To understand the need of collection and work with
different collections.
4
Topic Objectives
11/05/22 5
What is Android ?
• It is an open source.
6
History of Android
7
Open Handset Alliance
8
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)
9
Features of Android
11
Android Architecture
12
DVM
13
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
14
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.
15
Manifest
• Every Android app must include an
AndroidManifest.xml file describing functionality
16
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
17
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
18
Examples of activities
19
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
20
Layouts and Activities
21
Activity lifecycle
22
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.
23
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
24
What is the Activity Lifecycle?
25
Activity states and app visibility
● Created (not visible yet)
● Started (visible)
● Resume (visible)
● Paused(partially invisible)
● Stopped (hidden)
● Destroyed (gone from memory)
27
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
28
Activity states and callbacks graph
29
Implementing and overriding callbacks
30
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()
31
onCreate(Bundle savedInstanceState)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
}
32
onStart() –> Started
33
onStart()
@Override
protected void onStart() {
super.onStart();
// The activity is about to become visible.
}
34
onRestart() –> Started
35
onRestart()
@Override
protected void onRestart() {
super.onRestart();
// The activity is between stopped and started.
}
36
onResume() –> Resumed/Running
37
onResume()
@Override
protected void onResume() {
super.onResume();
// The activity has become visible
// it is now "resumed"
}
38
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
39
onPause()
@Override
protected void onPause() {
super.onPause();
// Another activity is taking focus
// this activity is about to be "paused"
}
40
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
41
onStop()
@Override
protected void onStop() {
super.onStop();
// The activity is no longer visible
// it is now "stopped"
}
42
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
43
onDestroy()
@Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
}
44
Activity instance
state
45
When does config change?
46
What happens on config change?
47
Activity instance state
● State information is created while the Activity is running,
such as a counter, user text, animation progression
48
Saving and
restoring
Activity state
49
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
50
Saving instance state
51
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()));
}
52
Restoring instance state
53
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);
}
}
54
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);
}
}
55
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.
56
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
11/05/22 57
Thank You
11/05/22 58