0% found this document useful (0 votes)
9 views24 pages

Applications

The document provides an overview of Android application components, specifically Activities and Fragments, detailing their roles and lifecycle methods. It explains how layouts and views are structured, including various layout types and the importance of the View and ViewGroup classes. Additionally, it covers tasks, the back stack concept, and the use of Intents for navigation and data transfer between activities.

Uploaded by

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

Applications

The document provides an overview of Android application components, specifically Activities and Fragments, detailing their roles and lifecycle methods. It explains how layouts and views are structured, including various layout types and the importance of the View and ViewGroup classes. Additionally, it covers tasks, the back stack concept, and the use of Intents for navigation and data transfer between activities.

Uploaded by

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

APPLICATIONS

ACTIVITIES AND FRAGMENTS


 Activity: An Activity is an application
component that provides a screen with which
users can interact in order to do something,
such as dial the phone, take a photo, send an
email, or view a map. Each activity is given a
window in which to draw its user interface.
The window typically fills the screen, but may
be smaller than the screen and float on top
of other windows.
ACTIVITIES AND FRAGMENTS
 Fragments: A Fragment is a piece of an
application's user interface or behavior that
can be placed in an Activity which enable
more modular activity design. It will not be
wrong if we say, a fragment is a kind of sub-
acitivity.
A fragment has its own layout and its own
behaviour with its own lifecycle callbacks.
 You can add or remove fragments in an activity
while the activity is running.
 A fragment can be used in multiple activities.
 You can combine multiple fragments in a single
activity to build a multi-pane UI.
ACTIVITIES AND FRAGMENTS
ACTIVITY LIFECYCLE
ACTIVITY LIFECYCLE
 onCreate() : This is the first callback and
called when the activity is first created.
 onStart() : This callback is called when the

activity becomes visible to the user.


 onResume() : This is called when the user

starts interacting with the application.


 onPause() : The paused activity does not

receive user input and cannot execute any


code and called when the current activity is
being paused and the previous activity is
being resumed.
ACTIVITY LIFECYCLE
 onStop() : This callback is called when the
activity is no longer visible.
 onDestroy() : This callback is called before

the activity is destroyed by the system.


 onRestart() : This callback is called when

the activity restarts after stopping it.

 Have a look here.


THE ONCREATE METHOD
 onCreate() is called when your Activity is
getting created for the first time. It is called
only once during the entire Activity Lifecycle.
One of the important things you are
supposed to do is to set the Activity Layout
through setContentView function.

 Also, you can use onCreate to initialize your


variables. In any Android application,
whenever you create an Activity, the
minimum method which you need to override
is onCreate.
THE ONCREATE METHOD
 Please refer following figure.
LAYOUTS AND VIEWS
 A layout defines the visual structure for a
user interface, such as the UI for an activity
or app widget. You can declare a layout in
two ways:
 Declare UI elements in XML. Android provides
a straightforward XML vocabulary that
corresponds to the View classes and subclasses,
such as those for widgets and layouts.
 Instantiate layout elements at runtime. Your
application can create View and ViewGroup
objects (and manipulate their properties)
programmatically.

There are following types of Layouts available.


LAYOUTS AND VIEWS
 Linear Layout : LinearLayout is a view
group that aligns all children in a single
direction, vertically or horizontally.
 Relative Layout : RelativeLayout is a view

group that displays child views in relative


positions.
 Table Layout : TableLayout is a view that

groups views into rows and columns.


 Absolute Layout : AbsoluteLayout enables

you to specify the exact location of its


children.
LAYOUTS AND VIEWS
 Frame Layout : The FrameLayout is a
placeholder on screen that you can use to
display a single view.
 List View : ListView is a view group that

displays a list of scrollable items.


 Grid View : GridView is a ViewGroup that

displays items in a two-dimensional,


scrollable grid.
LAYOUTS AND VIEWS
 The basic building block for user interface is
a View object which is created from the View
class and occupies a rectangular area on the
screen and is responsible for drawing and
event handling. View is the base class for
widgets, which are used to create interactive
UI components like buttons, text fields, etc.

 The ViewGroup is a subclass of View and


provides invisible container that hold other
Views or other ViewGroups and define their
layout properties.
LAYOUTS AND VIEWS
 At third level we have different layouts which
are subclasses of ViewGroup class and a
typical layout defines the visual structure for
an Android user interface and can be created
either at run time using View/ViewGroup
objects or you can declare your layout using
simple XML file main_layout.xml which is
located in the res/layout folder of your
project.
THE FINDVIEWBYID METHOD
 public View findViewById (int id) : Finds a
view that was identified by the id attribute
from the XML that was processed in
onCreate(Bundle).
 Returns the view if found or null otherwise.

 It just looks through out view hierarchy and

returns reference to a view with requested


viewId.
TASKS AND THE "BACK STACK"
 A task is a collection of activities that users
interact with when performing a certain job.
The activities are arranged in a stack (the
back stack), in the order in which each
activity is opened.
TASKS AND THE "BACK STACK"
 A representation of how each new activity in
a task adds an item to the back stack. When
the user presses the Back button, the current
activity is destroyed and the previous activity
resumes.
 If the user continues to press Back, then

each activity in the stack is popped off to


reveal the previous one, until the user
returns to the Home screen (or to whichever
activity was running when the task began).
When all activities are removed from the
stack, the task no longer exists.
TASKS AND THE "BACK STACK"
 When Activity A starts Activity B, Activity A is
stopped, but the system retains its state (such
as scroll position and text entered into forms).
If the user presses the Back button while in
Activity B, Activity A resumes with its state
restored.
 When the user leaves a task by pressing

the Home button, the current activity is


stopped and its task goes into the background.
The system retains the state of every activity
in the task. If the user later resumes the task
by selecting the launcher icon that began the
task, the task comes to the foreground and
resumes the activity at the top of the stack.
TASKS AND THE "BACK STACK"
 If the user presses the Back button, the
current activity is popped from the stack and
destroyed. The previous activity in the stack
is resumed. When an activity is destroyed,
the system does not retain the activity's
state.
 Activities can be instantiated multiple times,

even from other tasks.


INTENTS AND RESULTS
 Android Intent lets you navigate from one
android activity to another.
 An Intent object is a bundle of information

which is used by the component that


receives the intent plus information used by
the Android system.
 An Intent object can contain the following

components based on what it is


communicating or going to perform.
 Android Intent can be defined as a simple

message objects which is used to


communicate from 1 activity to another.
INTENTS AND RESULTS
 Intents define intention of an Application .
They are also used to transfer data between
activities.
 An Android Intent can be used to perform

following 3 tasks :
 Open another Activity or Service from the current
Activity
 Pass data between Activities and Services
 Delegate responsibility to another application.
For example, you can use Intents to open the
browser application to display a URL.
 Types of Intents
 EXPLICIT INTENTS
 IMPLICIT INTENTS
INTENTS AND RESULTS
 Starting another activity doesn't have to be
one-way. You can also start another activity
and receive a result back. To receive a result,
call startActivityForResult() (instead of
startActivity()).
 Of course, the activity that responds must be

designed to return a result. When it does, it


sends the result as another Intent object.
Your activity receives it in the
onActivityResult() callback.
 Have a look here.
STARTACTIVITY AND RELATED
METHODS
 You can start another activity of another
application by calling startActivity(), passing
it an Intent that has the activity action name
defined within <intent-filter> in the
AndroidManifest.xml.
 Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
 This line of code will create SecondActivity
screen and display it to user. Now user can
interact with it.
 If user press back button SecondActivity screen
will be terminated and user will come to previous
screen.
STARTACTIVITY AND RELATED
METHODS
startActivityForResult :
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);

protected void onActivityResult(int requestCode, int


resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult

You might also like