Chapter - 1 (Mad)

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 57

UNIVERSITY INSTITUTE OF

COMPUTING

Mobile Application Development

Prepared By: Ruchika

UNIT-1 DISCOVER . LEARN .


EMPOWER
Summer Industrial Training
on
MOBILE APPLICATION
DEVLOPMENT

07/28/24 2
Importance

• Students are able to create and run activities.


• Students are able to work with different activities interaction.
• Students are able to implement programs based on Services in
android.
• Students are able to understand the bunder while using multiple
activities.
• Students are able work with different methods of activity life
cycle.
• Students are able to understand the need of Broadcast receivers
in android development.
3

07/28/24
Topic Objectives

• To understand different android constructs.


• To work with activities and switching between them.
• To implement the life cycle methods of activity.
• To understand the need of Bundles and intents.
• To work with Services.
• To understand the Content Provider and Broadcast Receivers.

07/28/24 4
What is Android ?

• It is an open source.

• Android is a software package and Linux-based


Operating System designed primarily for touch
screen mobile devices such as Smart Phones and
Tablet Computers.

• In other words, we can say that it is a mobile


operating system based on Linux kernel.

5
History of Android

• Initially, Andy Rubin founded Android Incorporation in


Palo Alto, California, United States in October, 2003.
• In 17th August 2005, Google acquired Android
Incorporation.
• The key employees of Android Incorporation other than
Andy Rubin are Rich Miner, Chris White and Nick
Sears.
• Developed and Managed by the OHA.

6
Open Handset Alliance

• It’s consortium (Association of two or more


individuals, organizations) of 84 companies.

• It was established on 6th November 2007

• This group of companies are allowed to use source


code of Android and develop applications.

• Reason for Nokia not to develop Android Mobiles is


Nokia is not part of OHA.

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

• Camera ,GPS, compass, accelerometer (hardware


dependent)
• It provides many interesting features like weather
details, opening screen, live (RSS) Really Simple
Syndication etc.
• It provides support for messaging services (SMS and
MMS), web browser, storage (SQLite) etc.
• Rich Development Environment including a device
emulator, tools for debugging, memory, performance
profiling and an plugin for the Eclipse IDE.
9
Android Version Images

10
Android Architecture

11
DVM

• Android apps execute on Dalvik


VM, a “clean-room” implementation
of JVM.
-- Dalvik optimized for efficient execution
-- Dalvik: register-based VM, unlike Oracle’s
stack-based JVM
-- Kotlin .class bytecode translated to Dalvik
EXecutable (DEX) bytecode, which Dalvik
interprets

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

• The manifest specifies:


– App’s Activities, Services, etc.
– Permissions requested by app
– Minimum API required
– Hardware features required, e.g., camera with
autofocus
– External libraries to which app is linked, e.g., Google
Maps library

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

● An Activity typically has a UI layout


● Layout is usually defined in one or more XML files
● Activity "inflates" layout as part of being created

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?

●The set of states an Activity can be in during its lifetime, from


when it is created until it is destroyed

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)

State changes are triggered by user action, configuration changes


such as device rotation, or system action
25
Activity lifecycle
callbacks

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

● Only onCreate() is required


● Override the other callbacks to change default behavior

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

●Called when the Activity is becoming visible to user


●Can be called more than once during lifecycle
●Followed by onResume() if the activity comes to the foreground,
or onStop() if it becomes hidden

32
onStart()

@Override
protected void onStart() {
super.onStart();
// The activity is about to become visible.
}

33
onRestart() –> Started

●Called after Activity has been stopped, immediately before it is


started again
●Transient state
●Always followed by onStart()

34
onRestart()

@Override
protected void onRestart() {
super.onRestart();
// The activity is between stopped and started.
}

35
onResume() –> Resumed/Running

●Called when Activity will start interacting with user


●Activity has moved to top of the Activity stack
●Starts accepting user input
●Running state
●Always followed by onPause()

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?

Configuration changes invalidate the current layout or other resources in your


activity when the user:
●Rotates the device
●Chooses different system language, so locale changes
●Enters multi-window mode (from Android 7)

45
What happens on config change?

On configuration change, Android:


1. Shuts down Activity
2. Starts Activity over again
by calling:
by calling:
● onPause() ● onCreate()
● onStop() ● onStart()
● onDestroy() ● onResume()

46
Activity instance state
● State information is created while the Activity is running, such
as a counter, user text, animation progression

● State is lost when device is rotated, language changes, back-


button is pressed, or the system clears memory

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

Implement onSaveInstanceState() in your Activity

●Called by Android runtime when there is a possibility the Activity


may be destroyed
●Saves data only for this instance of the Activity during current
session

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

Two ways to retrieve the saved Bundle


●in onCreate(Bundle mySavedState)
Preferred, to ensure that your user interface, including any saved state, is
back up and running as quickly as possible
●Implement callback (called after onStart())
onRestoreInstanceState(Bundle mySavedState)

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

You might also like