Beruflich Dokumente
Kultur Dokumente
Session 1
Activity Life cycle
i) Understanding the Life cycle methods
ii) Working example of life cycle
What is Activity ?
The entire lifetime of an activity happens between the first call to onCreate(Bundle) through to a single
final call to onDestroy(). An activity will do all setup of "global" state in onCreate(), and release all
remaining resources in onDestroy().
For example, if it has a thread running in the background to download data from the network, it may
create that thread in onCreate() and then stop the thread in onDestroy().
The visible lifetime of an activity happens between a call to onStart() until a corresponding call to
onStop(). During this time the user can see the activity on-screen, though it may not be in the foreground
and interacting with the user. Between these two methods you can maintain resources that are needed to
show the activity to the user. For example, you can register a BroadcastReceiver in onStart() to monitor
for changes that impact your UI, and unregister it in onStop() when the user no longer sees what you are
displaying. The onStart() and onStop() methods can be called multiple times, as the activity becomes
visible and hidden to the user.
@COPYRIGHTS BY Dhiraj P. karalkar
Dhiraj P.karalkar
The foreground lifetime of an activity happens between a call to onResume() until a corresponding call
to onPause(). During this time the activity is in front of all other activities and interacting with the user.
An activity can frequently go between the resumed and paused states -- for example when the device goes
to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these
methods should be fairly lightweight.
The following diagram shows the important state paths of an Activity.
What is context ?
As the name suggests, its the context of current state of the application/object. It lets newly created
objects understand what has been going on. Typically you call it to get information regarding another part
of your program (activity, package/application)
Context is a reference to current object , also context allows access to information about application
environment
What is Bundle ?
Bundle generally use for passing data between various Activities. It depends on you what type of values
you want to pass but bundle can hold all types of values and pass to the new activity.
Budle is to hold the bunch of various objects .
what is AndroidManifest.xml ?
This file contains all the information related to an Application
Every application must have an AndroidManifest.xml file (with precisely that name) in its root directory.
The manifest presents essential information about the application to the Android system, information
some more information regarding Android Manifest,xml
It names the Java package for the application. The package name serves as a unique identifier for
the application.
It describes the components of the application the activities, services, broadcast receivers, and
content providers that the application is composed of. It names the classes that implement each of
the components and publishes their capabilities (for example, which Intent messages they can
handle). These declarations let the Android system know what the components are and under
what conditions they can be launched.
It determines which processes will host application components.
It declares which permissions the application must have in order to access protected parts of the
API and interact with other applications.
It also declares the permissions that others are required to have in order to interact with the
application's components.
It lists the Instrumentation classes that provide profiling and other information as the application
is running. These declarations are present in the manifest only while the application is being
developed and tested; they're removed before the application is published.
It declares the minimum level of the Android API that the application requires.
It lists the libraries that the application must be linked against.
Dhiraj P.karalkar
onCreate();
onStart();
onResume();
onPause();
onStop();
onDestroy();
onRestart();
Dhiraj P.karalkar
This is because when user clicks HOME button of the Device an LifeCycleActivity wasnt destroyed
Note :so these scenario you must take in the account , whether an Activity is destroyed or stopped
Session 2
Application development (Hello world)
Creating first android application
Save Activity state onSaveInstanceState()
Restore Activity state onRestoreInstanceState()
Debugging an Application
DDMS and log results
Here are the following steps to create your first Android Application
1)Go to File >> New >> Android Application Project
show in the following fig below
Dhiraj P.karalkar
Dhiraj P.karalkar
Dhiraj P.karalkar
Debugging an Application
The Debug Perspective in Eclipse gives you access to the following tabs:
Debug - Displays previously and currently debugged Android applications and its currently
running threads
Variables - When breakpoints are set, displays variable values during code execution
Breakpoints - Displays a list of the set breakpoints in your application code
LogCat - Allows you to view system log messages in real time. The LogCat tab is also available
in the DDMS perspective.
You can access the Debug Perspective by clicking Window > Open Perspective > Debug. Refer to the
appropriate documentation for the Eclipse debugger for more information.
Devices - Shows the list of devices and AVDs that are connected to ADB.
Emulator Control - Lets you carry out device functions.
LogCat - Lets you view system log messages in real time.
Threads - Shows currently running threads within a VM.
Heap - Shows heap usage for a VM.
Allocation Tracker - Shows the memory allocation of objects.
File Explorer - Lets you explore the device's file system.
To access the DDMS perspective, go to Window > Open Perspective > DDMS. If DDMS does not
appear, go to Window > Open Perspective > Other ... and select DDMS from the Open Perspective
window that appears. For more information on using DDMS, see Using the Dalvik Debug Monitor
Server.
Dhiraj P.karalkar
Session3
Working with Layouts techniques
The different layouts in Android are
1 Linear Layout
2. Relative Layout
3. Table Layout
Android allows you to create view layouts using simple XML file (we can also create a layout
using java code). All the layouts must be placed in /res/layout folder.
Dhiraj P.karalkar
10
Linear Layout
Linear Layout that arranges its children in a single column or a single row.
To configure a LinearLayout, you have five main areas of control besides the container's contents:
Orientation sets the vertical and horizontal orientation
fill model fills the layouts accordingly (fill parent , wrap context )
gravity - top| bottom| center
padding - The padding is expressed in pixels for the left, top, right and bottom parts of the view
margin - The margin is expressed in pixels spacing for the left, top, right and bottom of the view
Dhiraj P.karalkar
11
res/layout -> Right Click -> New -> Android XML File
3. Now open newly created xml file (in my case linear_layout.xml) and type the following code.
Here is the linear_layout.xml file
4. To set this newly created view as the initial view of your app, Open your MainActivity.java file. You
would see the following line inside the onCreate function setContentView(R.layout.main). Change
R.layout.main to R.layout.yourlinearviewname. In my case its R.layout.linear_layout
5. To run the application, right click on the project -> Run As -> 1. Android Application. You should
@COPYRIGHTS BY Dhiraj P. karalkar
Dhiraj P.karalkar
12
Dhiraj P.karalkar
13
2. Relative Layout
In a relative layout every element arranges itself relative to other elements or a parent element.
As an example, lets consider the layout defined below. The Cancel button is placed relatively, to the
right of the Login button parallely. Here is the code snippet that achieves the mentioned alignment
(Right of Login button parallely)
Dhiraj P.karalkar
14
4. Same like before open your MainActivity.java file and set the layout to your newly created relative
layout file. In my case its R.layout.relative_layout
setContentView(R.layout.relative_layout);
5. To run the application, right click on the project -> Run As -> 1. Android Application. You should
see your newly created relative layout in the emulator.
Dhiraj P.karalkar
15
Dhiraj P.karalkar
16
Session 4
Intents understanding
i) Implicit intents
ii) Explicit Intent
iii) Launching native application using Intent
iv) Switch between activities using Explicit Intent
v) Data transferring between activities using Intents
vi) Role of intent Filters
vii) Intent Filters in your manifest
What is an Intent is ??
An Intent is exactly what it describes. It's an "intention" to do an action.
An Intent is basically a message to say you did or want something to happen. Depending on the intent,
apps or the OS might be listening for it and will react accordingly.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to
launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and
startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background
Service
There are two types of an intents
i) Implicit intents
ii)Explicit Intents
i) Implicit intents
Implicit Intents have not specified a component; instead, they must include enough information for the
system to determine which of the available components is best to run for that intent.
To call or use the native Application of an Android a typical Implicit Intents are used
By using an Implicit intents we can call direct to the Contact , we can use a native web application and so
on .
Refer http://developer.android.com/reference/android/content/Intent.html
To have a more action/data pairs
Here is the example to open an Android native contact app
Dhiraj P.karalkar
17
Dhiraj P.karalkar
18
ii)Explicit Intents
In an Explicit intent, you actually specify the activity that is required to respond to the intent. In other
words, you explicitly designate the target component. This is typically used for application internal
messages.
In an Implicit intent (the main power of the android design), you just declare an intent and leave it to the
platform to find an activity that can respond to the intent. Here, you do not declare the target
component and hence is typically used for activating components of other applications seamlessly
This example has 2 activities:
1. FirstActivity
2. SecondActivity
The FirstActivity has a button "Invoke Next Activity" which when clicked explicitly calls the
" SecondActivity " class.
The relevant part of the code is here:
Note : In this scenario do not forget to add the SecondActivity in the manifest as shown
below
Dhiraj P.karalkar
19
Intent plays a vital role to send the data across the application ,
Intent is used to send the data from an activity to another Activity , we needs to add the key value pair to
send the data across the Application .
Here we are continuing with the previous example shows how to send the data across an activity , in this
example I have shown how to send the string from FirstActivity to SecondActivity this is the code for the
same
In this example on buttonclick event we are sending the string Dhiraj along with
Data from first activity , this string data is being collected at the SecondActivity using an Intent
as shown in the following source code
Dhiraj P.karalkar
20
As the example shown above data is received at the SecondActivity intent object is collected by using the
getIntent() method of an Activity class , these both the string are collected and shown in the textview
shown below at the output
Dhiraj P.karalkar
21
<action android:name="android.intent.action.MAIN" /> is the standard MAIN action is a main entry point
<category android:name="android.intent.category.LAUNCHER" /> the LAUNCHER category says that
this entry point should be listed in the application launcher
To any activity if you want to make it the launcher activity you need to mention the MAIN and the
LAUNCHER in the Manifest file
Dhiraj P.karalkar