Anatomy of an Android ApplicationAnatomy of an Android Application
Presented By:Presented By:
Nikunj DhameliyaNikunj Dhameliya
Student @ gvpStudent @ gvp
Important Android Terminology
Context :
The context is the central command center for an Android
application.
All application-specific functionality can be accessed
through the context.
Activity:
An Android application is a collection of tasks, each of which
is called an Activity.
Each Activity within an application has a unique task or
purpose.
Contd...
Intent:
The Android operating system uses an asynchronous
messaging mechanism to match task requests with the appropriate
Activity.
Each request is packaged as an Intent.
Service:
Tasks that do not require user interaction can be
encapsulated in a service.
A service is most useful when the operations are lengthy
(offloading time-consuming processing) or need to be done
regularly (such as checking a server for new mail).
Application Context
➢ Application Context is the central location for all top-level
application functionality.
➢ Context class can be used to manage application-specific
configuration details as well as application-wide operations and
data.
➢ Use the application Contextto access settings and resources
shared across multiple Activityinstances.
How we can Retrieve Application Context
➢ You can retrieve the Context for the current process using the
getApplicationContext() method
Example:
Context context getApplicationContext();
Using Application Context
➢ Retrieving Application Resources
✗ You can retrieve application resources using the getResources()
method of the application Context.
✗ The most straightforward way to retrieve a resource is by using its
resource identifier, a unique number automatically generated within
the R.java class.
✗ The following example retrieves a String instance from the
application resources by its resource ID:
String greeting getResources().getString(R.string.hello);
Contd..
➢ Accesses Application Preferences
✗ You can retrieve shared application preferences using the
getSharedPreferences()method of the application
Context.
✗ The SharedPreferences class can be used to save
simple application data, such as configuration settings.
Contd...
➢ Accessing other Application Functionality Using
Context
The application Context provides access to a number of other top-
level application features.
Here are a few more things you can do with the application
Context:
✗ Launch Activity instances
✗ Retrieve assets packaged with the application
✗ Request a system service (for example, location service)
✗ Manage private application files, directories, and databases
✗ Inspect and enforce application permissions
Performing Application Tasks with Activities
➢ The Android Activity class (android.app.Activity) is core to
any Android application.
➢ Much of the time, you define and implement an Activity
class for each screen in your application.
For example, a simple game application might have
the following five Activities, as shown as below
Contd....
A Simple Game with Five Activities
Contd..
➢ A Startup or Splash screen:
This activity serves as the primary entry point to the application. It displays
the application name and version information and transitions to the Main menu after a short
interval.
➢ A Main Menu screen:
This activity acts as a switch to drive the user to the core
Activities of the application. Here the users must choose what they want to do
within the application.
➢ A Game Play screen:
This activity is where the core game play occurs.
➢ A High Scores screen:
This activity might display game scores or settings.
➢ A Help/About screen:
This activity might display the information the user might
need to play the game.
Life Cycle of an Android Activity
➢ Android applications can be multi-process, and the Android operating
system allows multiple applications to run concurrently, provided
memory and processing power is available.
➢ Applications can have background processes, and 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.
➢ The Android operating system keeps track of all Activity objects running
by placing them on an Activity 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, that Activity is removed
from the activity stack, and the previous Activity in the stack resumes.
Contd...
I am the top Activity.
User can see and interact with me!
I am the second Activity in the stack.
If the user hits Back or the top Activity is destroyed,
the user can see and interact with me again!
I am an Activity in the middle of the stack.
Users cannot see and interact with me until everyone
above me is destroyed.
I am an Activity at the bottom of the stack.
If those Activities above me use too many
resources, I will be destroyed!
The Activity stack.
Contd...
➢ Android applications are responsible for managing their state
and their memory, resources, and data.
➢ They must pause and resume seamlessly.
➢ Understanding the different states within the Activity life cycle
is the first step to designing and developing robust Android
applications.
Contd...
➢ Using Activity Callbacks to manage Application State and
Resources
✗ Different important state changes within the Activity lifecycle are
punctuated by a series of important method callbacks.
✗ Here are the method stubs for the most important callbacks of the
Activity class:
public class MyActivity extends Activity {
protected void onCreate(Bundle savedInstanceState);
protected void onStart();
protected void onRestart();
Contd..
protected void onResume();
protected void onPause();
protected void onStop();
protected void onDestroy();
}
Contd..
Initializing Static Activity Data in onCreate()
➢ When an Activity first starts, the onCreate() method is called.
➢ The onCreate() method has a single parameter, a Bundle, which is null
if this is a newly started Activity.
➢ If this Activity was killed for memory reasons and is now restarted,
the Bundle contains the previous state information for this Activity so
that it can reinitiate.
➢ It is appropriate to perform any setup, such as layout and data
binding, in the onCreate() method.This includes calls to the
setContentView() method.
Initializing and Retrieving Activity Data in onResume()
➢ When the Activity reaches the top of the activity stack and becomes
the foreground process, the onResume() method is called.Although
the Activity might not be visible yet to the user, this is the most
appropriate place to retrieve any instances to resources (exclusive or
otherwise) that the Activity needs to run.
➢ Often, these resources are the most process-intensive, so we only keep
these around while the Activity is in the foreground.
Stopping, Saving, and Releasing Activity Data in
onPause()
➢ When another Activity moves to the top of the activity stack, the
current Activity is informed that it is being pushed down the activity
stack by way of the onPause() method.
➢ Here, the Activity should stop any audio, video, and animations it
started in the onResume() method.
➢ This is also where you must deactivate resources such as database
Cursor objects if you have opted to manage them manually.
Avoiding Activity Objects Being Killed
➢ Under low-memory conditions, the Android operating system
can kill the process for any Activity that has been paused,
stopped, or destroyed.
➢ This essentially means that any Activity not in the foreground is
subject to a possible shutdown.
➢ If the Activity is killed after onPause(), the onStop() and
onDestroy() methods might 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.
Saving Activity State into a Bundle with
onSaveInstanceState()
➢ 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 using the onSaveInstanceState() callback
method.
➢ This call is not guaranteed under all circumstances, so use the
onPause() method for essential data commits.
➢ When this Activity is returned to later, this Bundle is passed into
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
method using the onRestoreInstanceState() callback.
Destroy Static Activity Data in
onDestroy()
➢ When an Activity is being destroyed, the onDestroy() method is
called.
➢ The onDestroy() method is called for one of two reasons:
The Activity has completed its life cycle voluntarily
The Activity is being killed by the Android operating
system because it needs the resources.
➢ The act of killing an Activity does not remove it from the
activity stack.
➢ Instead, the Activity state is saved into a Bundle object.
Managing Activity Transitions with Intents
➢ In the course of the lifetime of an Android application, the user might
transition between a number of different Activity instances. At times,
there might be multiple Activity instances on the activity stack.
Developers need to pay attention to the life cycle of each Activity
during these transitions.
➢ Some Activity instance such as the application splash/startup scree
are shown and then permanently discarded when the Main menu
screen Activity takes over.
➢ Other Activity transitions are temporary, such as a child Activity
displaying a dialog box, and then returning to the original Activity. In
this case, the parent Activity launches the child Activity and expects
a result.
Transitioning Between Activities with Intents
➢ As previously mentioned,Android applications can have multiple
entry points.There is no main() function, such as you find in iPhone
development. Instead, a specific Activity can be designated as the
main Activity to launch by default within the AndroidManifest.xml
file.
➢ Other Activities might be designated to launch under specific
circumstances. For example, a music application might designate a
generic Activity to launch by default from the Application menu, but
also define specific alternative entry point Activities for accessing
specific music playlists by playlist ID or artists by name.
Launching a New Activity by Class Name
➢ You can start activities in several ways.The simplest method is to use
the Application Context object to call the startActivity() method,
which takes a single parameter, an Intent.
➢ For now, though,we focus on Intents and how they are used with
Activities.The following line of code calls the startActivity() method
with an explicit Intent.This Intent requests the launch of the target
Activity named MyDrawActivity by its class.
➢ This class is implemented elsewhere within the package.
startActivity(new Intent(getApplicationContext(), MyDrawActivity.class));
Creating Intents with Action and Data
➢ You’ve seen the simplest case to use an Intent to launch a class by name.
Intents need not specify the component or class they want to launch
explicitly. Instead, you can create an Intent Filter and register it within the
Android Manifest file.The Android operating system attempts to resolve the
Intent requirements and launch the appropriate Activity based on the filter
criteria.
➢ The guts of the Intent object are composed of two main parts: the action to
be performed and the data to be acted upon.You can also specify
action/data pairs using Intent Action types and Uri objects.
➢ The most common action types are defined in the Intent class, including
ACTION_MAIN (describes the main entry point of an Activity) and
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, such as the Browser or Phone Dialer.
Launching an Activity Belonging to Another
Application
➢ Initially, your application might be starting only Activities defined
within its own package.
➢ However, with the appropriate permissions, applications might also
launch external Activities within other applications. For example, a
Customer Relationship Management (CRM) application might launch
the Contacts application to browse the Contact database, choose a
specific contact, and return that Contact’s unique identifier to the CRM
application for use.
➢ Here is an example of how to create a simple Intent with a predefined
Action (ACTION_DIAL) to launch the Phone Dialer with a specific
phone number to dial in the form of a simple Uri object:
➢ Uri number Uri.parse(tel:5555551212);
Intent dial new Intent(Intent.ACTION_DIAL, number);
startActivity(dial);
Passing Additional Information Using Intents
➢ You can also include additional data in an Intent.The Extras property
of an Intent is stored in a Bundle object.The Intent class also has a
number of helper methods for getting and setting name/value pairs for
many common datatypes.
➢ For example, the following Intent includes two extra pieces of
information a string value and a boolean:
Intent intent new Intent(this, MyActivity.class);
intent.putExtra(“SomeStringData”,”Foo”);
intent.putExtra(“SomeBooleanData”,false);
Thank you..

Anatomy of android application

  • 1.
    Anatomy of anAndroid ApplicationAnatomy of an Android Application Presented By:Presented By: Nikunj DhameliyaNikunj Dhameliya Student @ gvpStudent @ gvp
  • 2.
    Important Android Terminology Context: The context is the central command center for an Android application. All application-specific functionality can be accessed through the context. Activity: An Android application is a collection of tasks, each of which is called an Activity. Each Activity within an application has a unique task or purpose.
  • 3.
    Contd... Intent: The Android operatingsystem uses an asynchronous messaging mechanism to match task requests with the appropriate Activity. Each request is packaged as an Intent. Service: Tasks that do not require user interaction can be encapsulated in a service. A service is most useful when the operations are lengthy (offloading time-consuming processing) or need to be done regularly (such as checking a server for new mail).
  • 4.
    Application Context ➢ ApplicationContext is the central location for all top-level application functionality. ➢ Context class can be used to manage application-specific configuration details as well as application-wide operations and data. ➢ Use the application Contextto access settings and resources shared across multiple Activityinstances.
  • 5.
    How we canRetrieve Application Context ➢ You can retrieve the Context for the current process using the getApplicationContext() method Example: Context context getApplicationContext();
  • 6.
    Using Application Context ➢Retrieving Application Resources ✗ You can retrieve application resources using the getResources() method of the application Context. ✗ The most straightforward way to retrieve a resource is by using its resource identifier, a unique number automatically generated within the R.java class. ✗ The following example retrieves a String instance from the application resources by its resource ID: String greeting getResources().getString(R.string.hello);
  • 7.
    Contd.. ➢ Accesses ApplicationPreferences ✗ You can retrieve shared application preferences using the getSharedPreferences()method of the application Context. ✗ The SharedPreferences class can be used to save simple application data, such as configuration settings.
  • 8.
    Contd... ➢ Accessing otherApplication Functionality Using Context The application Context provides access to a number of other top- level application features. Here are a few more things you can do with the application Context: ✗ Launch Activity instances ✗ Retrieve assets packaged with the application ✗ Request a system service (for example, location service) ✗ Manage private application files, directories, and databases ✗ Inspect and enforce application permissions
  • 9.
    Performing Application Taskswith Activities ➢ The Android Activity class (android.app.Activity) is core to any Android application. ➢ Much of the time, you define and implement an Activity class for each screen in your application. For example, a simple game application might have the following five Activities, as shown as below
  • 10.
    Contd.... A Simple Gamewith Five Activities
  • 11.
    Contd.. ➢ A Startupor Splash screen: This activity serves as the primary entry point to the application. It displays the application name and version information and transitions to the Main menu after a short interval. ➢ A Main Menu screen: This activity acts as a switch to drive the user to the core Activities of the application. Here the users must choose what they want to do within the application. ➢ A Game Play screen: This activity is where the core game play occurs. ➢ A High Scores screen: This activity might display game scores or settings. ➢ A Help/About screen: This activity might display the information the user might need to play the game.
  • 12.
    Life Cycle ofan Android Activity ➢ Android applications can be multi-process, and the Android operating system allows multiple applications to run concurrently, provided memory and processing power is available. ➢ Applications can have background processes, and 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. ➢ The Android operating system keeps track of all Activity objects running by placing them on an Activity 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, that Activity is removed from the activity stack, and the previous Activity in the stack resumes.
  • 13.
    Contd... I am thetop Activity. User can see and interact with me! I am the second Activity in the stack. If the user hits Back or the top Activity is destroyed, the user can see and interact with me again! I am an Activity in the middle of the stack. Users cannot see and interact with me until everyone above me is destroyed. I am an Activity at the bottom of the stack. If those Activities above me use too many resources, I will be destroyed! The Activity stack.
  • 14.
    Contd... ➢ Android applicationsare responsible for managing their state and their memory, resources, and data. ➢ They must pause and resume seamlessly. ➢ Understanding the different states within the Activity life cycle is the first step to designing and developing robust Android applications.
  • 15.
    Contd... ➢ Using ActivityCallbacks to manage Application State and Resources ✗ Different important state changes within the Activity lifecycle are punctuated by a series of important method callbacks. ✗ Here are the method stubs for the most important callbacks of the Activity class: public class MyActivity extends Activity { protected void onCreate(Bundle savedInstanceState); protected void onStart(); protected void onRestart();
  • 16.
    Contd.. protected void onResume(); protectedvoid onPause(); protected void onStop(); protected void onDestroy(); }
  • 17.
  • 18.
    Initializing Static ActivityData in onCreate() ➢ When an Activity first starts, the onCreate() method is called. ➢ The onCreate() method has a single parameter, a Bundle, which is null if this is a newly started Activity. ➢ If this Activity was killed for memory reasons and is now restarted, the Bundle contains the previous state information for this Activity so that it can reinitiate. ➢ It is appropriate to perform any setup, such as layout and data binding, in the onCreate() method.This includes calls to the setContentView() method.
  • 19.
    Initializing and RetrievingActivity Data in onResume() ➢ When the Activity reaches the top of the activity stack and becomes the foreground process, the onResume() method is called.Although the Activity might not be visible yet to the user, this is the most appropriate place to retrieve any instances to resources (exclusive or otherwise) that the Activity needs to run. ➢ Often, these resources are the most process-intensive, so we only keep these around while the Activity is in the foreground.
  • 20.
    Stopping, Saving, andReleasing Activity Data in onPause() ➢ When another Activity moves to the top of the activity stack, the current Activity is informed that it is being pushed down the activity stack by way of the onPause() method. ➢ Here, the Activity should stop any audio, video, and animations it started in the onResume() method. ➢ This is also where you must deactivate resources such as database Cursor objects if you have opted to manage them manually.
  • 21.
    Avoiding Activity ObjectsBeing Killed ➢ Under low-memory conditions, the Android operating system can kill the process for any Activity that has been paused, stopped, or destroyed. ➢ This essentially means that any Activity not in the foreground is subject to a possible shutdown. ➢ If the Activity is killed after onPause(), the onStop() and onDestroy() methods might 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.
  • 22.
    Saving Activity Stateinto a Bundle with onSaveInstanceState() ➢ 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 using the onSaveInstanceState() callback method. ➢ This call is not guaranteed under all circumstances, so use the onPause() method for essential data commits. ➢ When this Activity is returned to later, this Bundle is passed into 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 method using the onRestoreInstanceState() callback.
  • 23.
    Destroy Static ActivityData in onDestroy() ➢ When an Activity is being destroyed, the onDestroy() method is called. ➢ The onDestroy() method is called for one of two reasons: The Activity has completed its life cycle voluntarily The Activity is being killed by the Android operating system because it needs the resources. ➢ The act of killing an Activity does not remove it from the activity stack. ➢ Instead, the Activity state is saved into a Bundle object.
  • 24.
    Managing Activity Transitionswith Intents ➢ In the course of the lifetime of an Android application, the user might transition between a number of different Activity instances. At times, there might be multiple Activity instances on the activity stack. Developers need to pay attention to the life cycle of each Activity during these transitions. ➢ Some Activity instance such as the application splash/startup scree are shown and then permanently discarded when the Main menu screen Activity takes over. ➢ Other Activity transitions are temporary, such as a child Activity displaying a dialog box, and then returning to the original Activity. In this case, the parent Activity launches the child Activity and expects a result.
  • 25.
    Transitioning Between Activitieswith Intents ➢ As previously mentioned,Android applications can have multiple entry points.There is no main() function, such as you find in iPhone development. Instead, a specific Activity can be designated as the main Activity to launch by default within the AndroidManifest.xml file. ➢ Other Activities might be designated to launch under specific circumstances. For example, a music application might designate a generic Activity to launch by default from the Application menu, but also define specific alternative entry point Activities for accessing specific music playlists by playlist ID or artists by name.
  • 26.
    Launching a NewActivity by Class Name ➢ You can start activities in several ways.The simplest method is to use the Application Context object to call the startActivity() method, which takes a single parameter, an Intent. ➢ For now, though,we focus on Intents and how they are used with Activities.The following line of code calls the startActivity() method with an explicit Intent.This Intent requests the launch of the target Activity named MyDrawActivity by its class. ➢ This class is implemented elsewhere within the package. startActivity(new Intent(getApplicationContext(), MyDrawActivity.class));
  • 27.
    Creating Intents withAction and Data ➢ You’ve seen the simplest case to use an Intent to launch a class by name. Intents need not specify the component or class they want to launch explicitly. Instead, you can create an Intent Filter and register it within the Android Manifest file.The Android operating system attempts to resolve the Intent requirements and launch the appropriate Activity based on the filter criteria. ➢ The guts of the Intent object are composed of two main parts: the action to be performed and the data to be acted upon.You can also specify action/data pairs using Intent Action types and Uri objects. ➢ The most common action types are defined in the Intent class, including ACTION_MAIN (describes the main entry point of an Activity) and 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, such as the Browser or Phone Dialer.
  • 28.
    Launching an ActivityBelonging to Another Application ➢ Initially, your application might be starting only Activities defined within its own package. ➢ However, with the appropriate permissions, applications might also launch external Activities within other applications. For example, a Customer Relationship Management (CRM) application might launch the Contacts application to browse the Contact database, choose a specific contact, and return that Contact’s unique identifier to the CRM application for use. ➢ Here is an example of how to create a simple Intent with a predefined Action (ACTION_DIAL) to launch the Phone Dialer with a specific phone number to dial in the form of a simple Uri object: ➢ Uri number Uri.parse(tel:5555551212); Intent dial new Intent(Intent.ACTION_DIAL, number); startActivity(dial);
  • 29.
    Passing Additional InformationUsing Intents ➢ You can also include additional data in an Intent.The Extras property of an Intent is stored in a Bundle object.The Intent class also has a number of helper methods for getting and setting name/value pairs for many common datatypes. ➢ For example, the following Intent includes two extra pieces of information a string value and a boolean: Intent intent new Intent(this, MyActivity.class); intent.putExtra(“SomeStringData”,”Foo”); intent.putExtra(“SomeBooleanData”,false);
  • 30.