0% found this document useful (0 votes)
3 views13 pages

Android 2 1

The document outlines the lifecycle of Android activities, detailing the sequence of callback methods including onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy(). It also describes the different types of layouts in Android, such as LinearLayout and RelativeLayout, and explains the concept of Intents, including implicit and explicit intents. Additionally, it covers how to create and handle menus in Android applications, including context menus and option menus.

Uploaded by

sstories569
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Android 2 1

The document outlines the lifecycle of Android activities, detailing the sequence of callback methods including onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy(). It also describes the different types of layouts in Android, such as LinearLayout and RelativeLayout, and explains the concept of Intents, including implicit and explicit intents. Additionally, it covers how to create and handle menus in Android applications, including context menus and option menus.

Uploaded by

sstories569
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1. How many callback methods are in android?

Explain their behavior at


different stages in Android.

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.

Following is the example of defining a onCreate() method in android activity.

@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.

Following is the example of defining a onStart() method in android activity.

@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.

Mostly the core functionality of an app is implemented in onResume() method.

In case if any interruption events happen in Resumed state, the activity will entered into Paused state
and the system will invoke onPause() method.

Following is the example of defining a onResume() method in android activity.

@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.

Following is the example of defining a onPause() method in android activity.

@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.

Following is the example of defining a onStop() method in android activity.

@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.

Following is the example of defining a onDestroy() method in android activity.

@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

onCreate(), onStart(), onPause(), onRestart(), onResume(), onStop() and onDestroy() to go


through a different stages of activity life cycle.

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.

The concepts of lifetimes and lifecycle methods are illustrated in Figure:

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

Android Linear Layout

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.

Android Relative Layout

In android, RelativeLayout is a ViewGroup which is used to specify the position of


child View instances relative to each other (Child A to the left of Child B) or relative to the parent
(Aligned to the top of parent).

6
Android Frame Layout

In android, FrameLayout is a ViewGroup subclass which is used to specify the position


of View instances it contains on the top of each other to display only single View inside the
FrameLayout.

Android Table Layout

In android, TableLayout is a ViewGroup subclass which is used to display the child View elements
in rows and columns.

Android Web View

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.

Android Grid View

In android, GridView is a ViewGroup which is used to display items in a scrollable grid of


columns and rows.

4. Differentiate between Linear and Relative Layout?

Feature LinearLayout RelativeLayout


Orientation Can be vertical or horizontal Always relative
Child views are stacked or placed next to Child views are positioned relative to each
Positioning each other other
Flexibility Less flexible More flexible
Complexity Less complex More complex
Performance Less performant than relative layout More performant than linear layout

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.

In android, intents are mainly used to perform following things.

 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

Here are two types of intents in android: implicit and explicit.

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.

6. How to create "menu" in Android, what are the different items of


"menu"?

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.

Following is the example of defining a menu in XML file (menu_example.xml).

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

android:id It is used to uniquely identify element in application.

android:icon It is used to set the item's icon from drawable folder.

android:title It is used to set the item's title

android:showAsAction It is used to specify how the item should appear as an action


item in the app bar.

7. Which method is used to handle item click event for popup menu
(onMenuItemClick())?

Handle Android Popup Menu Click Events

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;
}
}

8. How to call the method "MenuInflater.inflate()" for "menu"? Write the


code.

Load Android Options Menu from an Activity

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.

Handle Android Menu Click Events


In android, we can handle a menu item click events using ItemSelected() event based on the menu
type. Following is the example of handling a context menu item click event
using onContextItemSelected().

@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())?

Handle Android Context Menu Click Events


we can handle a context menu item click events using onContextItemSelected() method.

Following is the example of handling a context menu item click event


using onContextItemSelected() method.

12
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Save") {
// do your coding
}
else {
return false;
}
return true;
}

11.Difference between context menu and option menu in android.

Feature Context menu Option menu Popup menu


When it When the user long-presses When the activity is When the user clicks on a button
appears an item launched or icon
Anchored to the item that was Appears at the top of Appears below the button or icon
Location long-pressed the screen that was clicked
Specific to the item that was Related to the button or icon that
Actions long-pressed Global to the activity was clicked

13

You might also like