Please answer every question with brief explanation 1.  The central command center for an Android application is known as thea.  Contextb.  Servicec.   Intentd.  Activity2.  The tasks in an Android application are referred to asa.  Intentsb.  Actionsc.   Activitiesd.  Services3.  For what purpose is a fragment commonly used?a.  To allow application components such as activities and services to communicate with one anotherb.  To measure the amount of battery usagec.   To avoid app crashes due to memory leaksd.  To hold the code and screen logic for placing the same user interface component in multiple screens4.  A resource identifier is a unique number that is generated where?a.  In the settings of the Eclipse IDEb.  Within the R.java classc.   Within the resources.* packaged.  Within the id.java class5.  The onCreate() method has a single parameter nameda.  Bufferb.  Devicec.   Bundled.  Partition6.  When the Activity reaches the top of the Activity stack and becomes the foreground process, what method is called?a. onDestroy()b. onRestart()c. onPause()d. onResume()7.
 If an Activity is vulnerable to being killed by the Android operating
system due to low memory, the Activity can save state information to a
Bundle object by using which callback method?a.  onStop()b.  isFinishing()c.   onSaveInstanceState()d.  killProcesses()8.
 When an Activity moves to the top of the Activity stack, the current
Activity is informed that it is being pushed down the Activity stack by
what method?a.  onPause()b.  onStop()c.   onResume()d.  onReload()9.  Tasks that do not require user interaction can be encapsulated in aa.  Serviceb.  Activityc.   Intentd.  Class10. You can retrieve application resources using which method of the application Context?a.  detectProperties()b.  getResources()c.   getAssets()d.  getAppResources()Refer below attached file and mobile application textbook  here https://bookshelf.vitalsource.com/#/
madd_ch5_v03.pdf

Unformatted Attachment Preview

Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Dr. Jose Garcia-Rubia
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Overview







Master important terminology
Learn what the application Context is and discover
how to use it
Accomplish tasks with activities and explore the
Activity lifecycle
Determine how to improve the overall structure of an
application using fragments
Manage Activity transitions and organize
navigation with intents
Discover the usefulness of services
Investigate other uses for intents
2
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Mastering Important Android
Terminology





Context
 android.content.Context
Activity
 android.app.Activity
Fragment
 android.app.Fragment
Intent
 android.content.Intent
Service
 android.app.Service
3
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
The Application Context



Central location for all top-level application
functionality
Used to manage application-specific configuration
details as well as application-wide operations and
data
Accesses settings and resources shared across
multiple Activity instances
4
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Retrieving the Application
Context
Context context = getApplicationContext();
5
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Using the Application Context







Retrieving application resources such as strings,
graphics, and XML files
Accessing application preferences
Managing private application files and directories
Retrieving uncompiled application assets
Accessing system services
Managing a private application database (SQLite)
Working with application permissions
6
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Warning!
Because the Activity class is derived from the
Context class, you can sometimes use the Activity
instead of retrieving the application Context explicitly.
However, don’t be tempted just to use your Activity
Context in all cases because doing so can lead to
memory leaks.
7
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Retrieving Application Resources
String greeting = getResources().getString(R.string.hello);
8
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Accessing Application Preferences
getSharedPreferences()
9
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Accessing Application Files and
Directories

You can use the application Context to access,
create, and manage application files and directories
private to the application as well as those on
external storage.
10
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Retrieving Application Assets
getAssets()
11
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Performing Application Tasks with
Activities

A simple game application might have the following
five activities:
1. Startup or splash screen
2. Main menu screen
3. Game play screen
4. High scores screen
5. Help/About screen
12
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Performing Application Tasks with
Activities (Cont’d)
13
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Lifecycle of an Android Activity





Android apps can be multiprocess.
Android allows multiple apps to run concurrently
(provided memory and processing power are
available).
Applications can have background behavior.
Applications can be interrupted and paused when
events such as phone calls occur.
There can be only one active application visible to
the user at a time—specifically, a single application
Activity is in the foreground at any given time.
Example: ActivityLifecycle.zip
https://developer.android.com/training/basics/activity-lifecycle/index.html
14
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Lifecycle of an Android Activity
(Cont’d)




Android keeps track of all Activity objects running
by placing them on an Activity stack.
The Activity stack is referred to as the “back
stack.”
When a new Activity starts, the Activity on the
top of the stack (the current foreground Activity)
pauses, and the new Activity pushes onto the top
of the stack.
When that Activity finishes, it is removed from the
Activity stack, and the previous Activity in the
stack resumes.
15
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Lifecycle of an Android Activity
(Cont’d)
16
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Using Activity Callbacks to
Manage App State and Resources
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState);
protected void onStart();
protected void onRestart();
protected void onResume();
protected void onPause();
protected void onStop();
protected void onDestroy();
}
17
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Lifecycle of an Android Activity
18
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Initializing Static Activity Data
in onCreate()




When an Activity first starts, onCreate() is called.
onCreate() has a single parameter, a Bundle (null if
newly started Activity).
If this Activity is a restarted Activity, Bundle contains
previous state information so that it can reinitiate.
Perform setup (layout and data binding), such as
setContentView(), in onCreate().
19
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Confirming Features in onStart()
When confirming features for the first time,
after the call to onCreate(), or
reconfirming after a call to onStop(), then
onRestart(), the onStart() method.
 To confirm that the appropriate features are
enabled on a user’s device.


For example, if Bluetooth is required for your application to function
properly, the onStart() method would check that Bluetooth has been
enabled, and if not, you would request that the user enable Bluetooth
before proceeding with your application.
20
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Initializing and Retrieving
Activity Data in onResume()



When the Activity reaches the top of the stack and
becomes the foreground process, onResume() is
called.
This is the most appropriate place to retrieve any
instances of resources (exclusive or otherwise) that
the Activity needs to run.
These resources are the most process intensive, so
we keep them around only while the Activity is in
the foreground.
21
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Tip
The onResume() method is often the
appropriate place to start audio, video, and
animations.
22
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Stopping, Saving, and Releasing
Activity Data in onPause()








When another Activity moves to the top of the stack, Activity is
informed and pushed down the stack by onPause().
Stop audio, video, and animations started in onResume().
Deactivate resources such as database Cursor or other objects that
should be cleaned up should your Activity be terminated.
onPause() may be the last chance for the Activity to clean up and
release any resources it does not need while in the background.
Save uncommitted data in case your application does not resume.
The system has a right to kill an Activity without further notice after
calling onPause().
Save state information to Activity-specific preferences or
application-wide preferences.
Perform anything in onPause() in a timely fashion. The new
foreground Activity is not started until onPause() returns.
23
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Warning!
Any resources and data retrieved in onResume()
should be released in onPause()! If they aren’t,
some resources may not be cleanly released if
the process is terminated!
24
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Avoiding Activities Being Killed


If the Activity is killed after onPause(), the
onStop() and onDestroy() methods will not be
called.
The more resources released by an Activity in the
onPause() method, the less likely the Activity is
to be killed while in the background without further
state methods being called.
25
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Saving Activity State with
onSaveInstanceState()


If an Activity is vulnerable to being killed by
Android, you can save state info to a Bundle with
onSaveInstanceState().
 This call is not guaranteed, so use onPause() for
essential data commits.
What is recommended?
 Save important data to persistent storage in
onPause(), but use onSaveInstanceState() to
start any data that can be used to rapidly restore
the current screen to the state it was in (as the
name of the method might imply) .
26
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Tip
You might want to use the onSaveInstanceState()
method to store nonessential information such as
uncommitted form field data or any other state
information that might make the user’s experience with
your application less cumbersome.
27
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Saving Activity State with
onSaveInstanceState() (Cont’d)



When this Activity is returned to later, this Bundle
is passed in to the onCreate() method, allowing the
Activity to return to the exact state it was in when
the Activity paused.
You can also read Bundle information after the
onStart() callback using
onRestoreInstanceState().
When the Bundle information is there, restoring the
previous state will be faster and more efficient than
starting from scratch.
28
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Destroying Static Activity Data
in onDestroy()


When an Activity is being destroyed in the normal
course of operation, the onDestroy() method is
called.
The onDestroy() method is called for one of two
reasons:
 The Activity completed its lifecycle voluntarily.
 The Activity is being killed by the OS because it
needs the resources (but still has the time to
gracefully destroy your Activity).
29
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Tip




isFinishing() returns false if the Activity has
been killed.
This method can be helpful in onPause() to know if
the Activity is not going to resume right away.
The Activity might still be killed in onStop() at a
later time.
You may be able to use this as a hint to know how
much instance state information to save or
permanently persist.
30
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Backward-Compatibile Activity
with AppCompatActivity


When a new version of Android is released, there are many new APIs
added, which are specifically designed for that version—and newer
versions—provided those features are not deprecated or removed in
future versions.
The Activity class has received frequent updates with new features.



The downside of that means those features will not work on older
versions of Android.
That is why AppCompatActivity class was introduced.
AppCompatActivity provides the same functionality as the Activity
class.
 It makes those same features available through the support library.
 It brings those new features to old versions of Android.
31
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Backward-Compatibile Activity with
AppCompatActivity (Cont’d)

The code samples provided with this book make frequent use of the
Activity class.
 In many cases, the AppCompatActivity is used to bring new
Activity feature support to older versions of Android.

Even though the APIs are nearly the same, there are minor differences
to their implementations.
 You will learn about those differences in the textbook and in the
code samples provided with the book.
 The code samples are available for download on the book’s
website (http://introductiontoandroid.blogspot.com).
When APIs are identical, we sometimes use Activity and
AppCompatActivity interchangeably, but where APIs are specific to
a certain implementation, we point this out.

32
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Backward-Compatibile Activity with
AppCompatActivity (Cont’d)


To use AppCompatActivity, simply extend your custom
Activity from AppCompatActivity instead of Activity and
import the class from
android.support.v7.app.AppCompatActivity.
You also need to add the appcompat-v7 support library as a
dependency to your Gradle build file.
 To learn how to add support libraries to your Gradle build
file, see appendix E, “Quick-Start: Gradle Build System,”
and the section titled “Configuring Application
Dependencies.”
33
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Organizing Activity
Components with Fragments




Android 3.0 introduced fragments.
A Fragment is a chunk of user interface with its own
lifecycle within an Activity.
 A fragment is represented by the Fragment class
(android.app.Fragment) and several supporting
classes.
A Fragment class instance must exist within an
Activity instance (and its lifecycle).
A Fragment need not be paired with the same
Activity class each time it’s instantiated.
34
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Organizing Activity Components
with Fragments (Cont’d)



Fragments are best illustrated by example.
Consider an MP3 music player app. Following the
one-screen-to-one-Activity rule:
 List Artists Activity
 List Artist Albums Activity
 List Album Tracks Activity
 Show Track Activity
Fine for a smartphone, but what about a tablet?
35
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Organizing Activity Components
with Fragments (Cont’d)
36
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Organizing Activity Components
with Fragments (Cont’d)

All four activities may fit on a single screen.
 Column 1 displays a list of artists.
 Selecting an artist filters the second column.
 Column 2 displays a list of that artist’s albums.
 Selecting an album filters the third column.
 Column 3 displays a list of that album’s tracks.
 The bottom half of the screen, below all of the
columns, displays the artist, album, or track art
and details.
37
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Organizing Activity Components
with Fragments (Cont’d)


We don’t want to build different activities for
different-size devices.
If you componentize your features and make four
fragments, you can mix and match them on the fly
while still having only one code base.
38
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Managing Activity Transitions
with Intents




Users transition between a number of different
Activity instances.
Developers need to pay attention to the Activity
lifecycle during these transitions.
Ways to handle permanently discarded Activity
transitions:
 startActivity() and finish()
Ways to handle temporary transitions with plans to
return:
 startActivityForResult() and
onActivityResult()
39
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Transitioning between Activities
with Intents



Android applications can have multiple entry points.
A specific Activity can be designated as the main
Activity to launch by default.
Other activities might be designated to launch under
specific circumstances.
40
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Launching a New Activity by
Class Name



You can start activities in several ways.
The simplest method:
 Use the Application Context object to call
startActivity().
 startActivity() takes a single parameter, an
Intent.
An Intent (android.content.Intent) is an
asynchronous message mechanism. It is used by
Android to match task requests with the appropriate
Activity or Service (launching it, if necessary)
and to dispatch broadcast Intent events to the
41
system at large.
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Launching a New Activity by
Class Name (Cont’d)
startActivity(new Intent(getApplicationContext(),
MyDrawActivity.class));
42
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Creating Intents with Action and
Data



The guts of the Intent object are composed of two
main parts:
 The action to be performed
 Optionally, the data to be acted upon
You can also specify action/data pairs using Intent
Action types and Uri objects.
Therefore, an Intent is basically saying “do this”
(the action) to “that” (the URI describing on what
resource the action is performed).
43
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Creating Intents with Action Data
(Cont’d)

The most common action types are defined in the
Intent class, including:
 ACTION_MAIN
 Describes the main entry point of an Activity
 ACTION_EDIT
Used in conjunction with a URI to the data
edited
You also find action types that generate integration
points with activities in other applications:
 The browser or Phone Dialer


44
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Launching an Activity
Belonging to Another Application

With the appropriate permissions, applications might
also launch external activities within other
applications.
 For example, a customer relationship
management (CRM) app might launch the
Contacts app to browse the Contacts database,
choose a specific contact, and return that
contact’s unique identifier to the CRM application
for use.
45
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Launching an Activity
Belonging to Another Application
(Cont’d)
Uri number = Uri.parse(“tel:5555551212”);
Intent dial = new Intent(Intent.ACTION_DIAL, number);
startActivity(dial);
46
Mobile Applications Design and Development
Chapter 5. Understanding Application Components
Launching an Activity Belonging to
Another Application (Cont’d)


You can find a list of commonly used Google
application intents at:
 http://d.android.com/guide/components/intentscommon.html
 http://www.openintents.org
A growing list of intents is available from third-party
applications and those within the Android SDK.
47
Mobile Applications Design and Development
Chapter 5. Understanding Applicati …
Purchase answer to see full
attachment