Android 2 1
Android 2 1
Android system initiates its program within an Activity starting with a call on onCreate() callback
method. There is a sequence of callback methods that start up an activity and a sequence of callback
methods that tear down an activity.
onCreate()
This is the first callback method and it fires when the system creates an activity for the first time. During
the activity creation, activity entered into a Created state.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Once onCreate() method execution is finished, the activity will enter into Started state and system
calls the onStart() method.
onStart()
The onStart() callback method will invoke when an activity entered into Started State by
completing onCreate()method. The onStart() method will make an activity visible to the user and
this method execution will finish very quickly.
@Override
protected void onStart()
{
super.onStart()
}
After completion of onStart() method execution, the activity enters into Resumed state and system
invoke the onResume() method.
1
onResume()
When an activity entered into Resumed state, the system invoke onResume() call back method. In this
state activity start interacting with user that means user can see the functionality and designing part
of an application on the single screen.
In case if any interruption events happen in Resumed state, the activity will entered into Paused state
and the system will invoke onPause() method.
@Override
public void onResume() {
super.onResume();
if (mCamera == null) {
initializeCamera();
}
}
If any interruption happen in Resumed state, the activity will entered into Paused state and the
system will invoke onPause() method.
onPause()
Whenever the user leaves an activity or the current activity is being Paused then the system
invoke onPause() method. The onPause() method is used to pause an operations like stop playing the
music when the activity is in paused state or pass an activity while switching from one app to another
app because every time only one app can be focused.
@Override
public void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
After completion of onPause() method execution, the next method is either onStop()
or onResume() depending on what happens after an activity entered into Paused state.
2
onStop()
The system will invoke onStop() callback method when an activity no longer visible to the user, the
activity will enter into Stopped state. This happens due to current activity entered into Resumed state
or newly launched activity covers complete screen or it’s been destroyed.
The onStop() method is useful to release all the app resources which are no longer needed to the user.
@Override
protected void onStop()
{
super.onStop();
}
The next callback method which raised by system is either onRestart(), in case if the activity coming
back to interact with the user or onDestroy(), in case if the activity finished running.
onRestart()
The system will invoke onRestart() method when an activity restarting itself after stopping it.
The onRestart() method will restore the state of activity from the time that is being stopped.
The onRestart() callback method in android activity will always followed by onStart() method.
onDestroy()
The system will invoke onDestroy() method before an activity is destroyed and this is the final callback
method which received by the android activity.
The system will invoke this onDestory() callback method either the activity is finishing or system
destroying the activity to save space.
@Override
public void onDestroy()
{
super.onDestroy();
}
The onDestroy() method will release all the resources which are not released by previous
callback onStop() method.
3
Android Activity Lifecycle Diagram
Generally, in android activity class uses different callback methods like
Following is the pictorial representation of Android Activity Life cycle which shows how Activity will
behave in different stages using callback methods.
4
2. Sketch the entire, visible and foreground lifetimes of android activity or
fragment transition during execution.
Lifetimes
The outline of the entire, visible, and foreground lifetimes through which an activity or fragment
transitions during execution are as below:
Entire Lifetime –The term “entire lifetime” is used to describe everything that takes place
between the initial call to the onCreate() method and the call to onDestroy() prior to the object
terminating.
Visible Lifetime – Covers the periods of execution between the call to onStart() and onStop().
During this period the activity or fragment is visible to the user though may not be the object
with which the user is currently interacting.
Foreground Lifetime – Refers to the periods of execution between calls to the onResume()
and onPause() methods.
It is important to note that an activity or fragment may pass through the foreground and visible
lifetimes multiple times during the course of the entire lifetime.
5
3. What is Android Layout? Briefly describe different types of Layouts
with respective diagram.
An Android layout is a structure that defines the UI of an Android app. It is made up of a hierarchy of
View and ViewGroup objects. View objects are the basic building blocks of an Android UI They are
responsible for drawing something the user can see and interact with, such as a button, text field,
or image.
We have a different type of layouts available in android to implement user interface for our android
applications with different designs based on our requirements.
Following are the commonly used layouts in android applications to implement required designs.
Linear Layout
Relative Layout
Frame Layout
Table Layout
Web View
List View
Grid View
In android, LinearLayout is a ViewGroup subclass which is used to render all child View instances
one by one either in horizontal direction or vertical direction based on the orientation property.
6
Android Frame Layout
In android, TableLayout is a ViewGroup subclass which is used to display the child View elements
in rows and columns.
In android, WebView is a browser which is used to display the web pages as a part of our activity
layout.
7
Android List View
In android, ListView is a ViewGroup which is used to display scrollable single column list of
items.
5. What is Android Intent for, and how many types of Intent are?
Intent is a messaging object which is used to request an action from another component.
Starting an Activity
Starting a Service
Delivering a Broadcast
8
There are two types of intents available in android, those are
1. Implicit Intents
2. Explicit Intents
1) Implicit Intent
Implicit Intent doesn't specify the component. In such case, intent provides information of
available components provided by the system that is to be invoked.
2) Explicit Intent
Explicit Intent specifies the component. In such case, intent provides the external class to be
invoked.
In android, Menu is a part of user interface (UI) component which is used to handle some
common functionality around the application. By using Menus in our applications, we can
provide better and consistent user experience throughout the application.
In android, to define menu, we need to create a new folder menu inside of our project resource
directory (res/menu/) and add a new XML file to build the menu with the following elements.
Element Description
<menu> It’s a root element to define a Menu in XML file and it will hold one or
more elements.
<item> It is used to create a menu item and it represent a single item in menu. This
element may contain a nested <menu> element in order to create a
submenu.
<group> It’s an optional and invisible for <item> elements. It is used to categorize
the menu items so they share properties such as active state and visibility.
9
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/mail"
android:icon="@drawable/ic_mail"
android:title="@string/mail" />
<item android:id="@+id/upload"
android:icon="@drawable/ic_upload"
android:title="@string/upload"
android:showAsAction="ifRoom" />
<item android:id="@+id/share"
android:icon="@drawable/ic_share"
android:title="@string/share" />
</menu>
The <item> element in menu supports different type of attributes to define item’s behavior and
appearance. Following are the some of commonly used <item> attributes in android applications.
Attribute Description
7. Which method is used to handle item click event for popup menu
(onMenuItemClick())?
To perform an action when the user selects a menu item, we need to implement
the PopupMenu.OnMenuItemClickListener interface and register it with our PopupMenu by
calling setOnMenuItemclickListener(). When the user selects an item, the system calls
the onMenuItemClick() callback in your interface.
Following is the example of handling a popup menu item click event using onMenuItemClick().
10
public void showMenu(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.archive:
archive(item);
return true;
case R.id.delete:
delete(item);
return true;
default:
return false;
}
}
To specify the options menu for an activity, we need to override onCreateOptionsMenu() method
and load the defined menu resource using MenuInflater.inflate() like as shown below.
@Override
public void onCreateOptionsMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_example, menu);
}
If we observe above code we are calling our menu using MenuInflater.inflate() method in the
form of R.menu.menu_file_name. Here our xml file name is menu_example.xml so we used file
name menu_example.
11
9. What is the method to handle a menu item click events? Write the code.
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mail:
// do something
return true;
case R.id.share:
// do something
return true;
default:
return super.onContextItemSelected(item);
}
}
If we observe above code, the getItemId() method will get the id of selected menu item based on
that we can perform our actions.
10.Which method is used to handle item click event for context menu
(onContextItemSelected())?
12
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Save") {
// do your coding
}
else {
return false;
}
return true;
}
13