0% found this document useful (0 votes)
35 views8 pages

Unit 3 (Ii)

App development material study

Uploaded by

Himanshi Sharma
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)
35 views8 pages

Unit 3 (Ii)

App development material study

Uploaded by

Himanshi Sharma
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/ 8

Android Application Components

Android applications are built using several key components, each serving a distinct purpose.
Here are the main components:

1. Activities: These represent a single screen with a user interface. Activities are the entry
point for interacting with the user and are implemented as subclasses of
the Activity class.

2. Services: These run in the background to perform long-running operations or to perform


work for remote processes. Services do not provide a user interface and are
implemented as subclasses of the Service class.

3. Broadcast Receivers: These components respond to broadcast messages from other


applications or from the system. They enable the application to respond to system-wide
broadcast announcements.

4. Content Providers: These manage shared application data. They provide mechanisms for
defining data security and for encapsulating the data. Content providers are
implemented as subclasses of the ContentProvider class.

5. Intents: These are messaging objects used to request an action from another app
component. Intents can be used to start activities, services, and broadcast receivers.

6. Widgets: These are small application views that can be embedded in other applications
(like the home screen) and receive periodic updates.

Activity Life Cycle

Typically, one activity in an application is specified as the "main" activity, which is presented to
the user when launching the application for the first time.

• Each activity can then start another activity in order to perform different actions. Each
time a new activity starts, the previous activity is stopped, but the system preserves
the activity in a stack (the "back stack").
• When a new activity starts, it is pushed onto the back stack and takes user focus. The
back stack abides to the basic "last in, first out" stack mechanism, so, when the user is
done with the current activity and presses the Back button, it is popped from the
stack (and destroyed) and the previous activity resumes. (The back stack is discussed
more in the Tasks and Back Stack document.)

• When an activity is stopped because a new activity starts, it is notified of this change
in state through the activity’s lifecycle callback methods.

• There are several callback methods that an activity might receive, due to a change in
its state—whether the system is creating it, stopping it, resuming it, or destroying it—
and each callback provides you the opportunity to perform specific work that’s
appropriate to that state change.

• For instance, when stopped, your activity should release any large objects, such as
network or database connections. When the activity resumes, you can reacquire the
necessary resources and resume actions that were interrupted. These state
transitions are all part of the activity lifecycle.

In Android application development, understanding the Activity Lifecycle is crucial for creating
robust and responsive apps. An activity represents a single screen with a user interface, and it
goes through various stages during its lifecycle. Here are the key stages and their corresponding
callback methods:
1. onCreate(): Called when the activity is first created. This is where you initialize your
activity, set up the UI, and bind data to lists.

2. onStart(): Invoked when the activity becomes visible to the user.

3. onResume(): Called when the activity starts interacting with the user. At this point, the
activity is at the top of the activity stack.

4. onPause(): Invoked when the system is about to start resuming another activity. This is
where you should save any unsaved data and stop animations or other ongoing actions
that don’t need to continue while the activity is in the background.

5. onStop(): Called when the activity is no longer visible to the user.

6. onDestroy(): Invoked before the activity is destroyed. This is the final call that the
activity receives.

Each of these methods allows you to manage the state of your activity and handle transitions
between states effectively.
Intents

An Intent is a messaging object you can use to request an action from another app component.
These are used to facilitate communication between different components of an application or
even between different applications. Here are the key points about intents:

Types of Intents

1. Explicit Intents: These specify the exact component to start by using the component’s
class name. They are typically used to start a component within the same application.

2. Implicit Intents: These do not specify a specific component. Instead, they declare a
general action to perform, allowing any app that can handle the action to respond. For
example, opening a web page or sharing a photo.

Common Uses

 Starting an Activity: You can start a new activity by passing an intent to startActivity(). If
you need a result back from the activity, use startActivityForResult().

 Starting a Service: Intents can start a service to perform background operations without
a user interface.

 Delivering a Broadcast: Intents can be used to send broadcasts to other apps or


components within the same app.

Example

Here’s a simple example of an explicit intent to start a new activity:

Intent intent = new Intent(this, SecondActivity.class);


startActivity(intent);

And an example of an implicit intent to open a web page:

Intent intent = new Intent(Intent.ACTION_VIEW);


intent.setData(Uri.parse("https://www.example.com"));
startActivity(intent);
Intent Filter

An intent filter is an expression in an app's manifest file that specifies the type of intents that
the component would like to receive. For instance, by declaring an intent filter for an activity,
you make it possible for other apps to directly start your activity with a certain kind of intent.
Likewise, if you do not declare any intent filters for an activity, then it can be started only with
an explicit intent.

Fragments

Fragments in Android are a powerful way to create modular and reusable components within
your app’s user interface. A fragment defines and manages its own layout, has its own lifecycle,
and can handle its own input events. Fragments can't live on their own. They must be hosted by
an activity or another fragment.

Some key points about fragments:

 Modularity and Reusability: Fragments allow you to divide your UI into discrete chunks
that can be managed independently. This makes it easier to reuse components across
different parts of your app.
 Lifecycle: Fragments have their own lifecycle, similar to activities. Key lifecycle methods
include onAttach(), onCreate(), onCreateView(), onActivityCreated(), onStart(), onResum
e(), onPause(), onStop(), onDestroyView(), and onDestroy()
 Hosting: Fragments must be hosted by an activity or another fragment. They can’t exist
on their own1.
 Flexible UI Design: Fragments are particularly useful for creating flexible UI designs that
adapt to different screen sizes and orientations. For example, you can use fragments to
create a multi-pane layout on tablets and a single-pane layout on phones2.
 Fragment Transactions: You can add, replace, or remove fragments dynamically at
runtime using fragment transactions. These transactions can be added to a back stack,
allowing users to navigate back through the changes.
 Communication: Fragments can communicate with their host activity and other
fragments using interfaces or shared ViewModels.

In the above figure, two versions of the same screen on different screen sizes. On the left, a
large screen contains a navigation drawer that is controlled by the activity and a grid list that is
controlled by the fragment. On the right, a small screen contains a bottom navigation bar that is
controlled by the activity and a linear list that is controlled by the fragment.
Need of Fragments in Android

• Before the introduction of Fragment’s we can only show a single Activity on the screen at
one given point of time so we were not able to divide the screen and control different
parts separately. With the help of Fragment’s we can divide the screens in different parts
and controls different parts separately.

• By using Fragments we can comprise multiple Fragments in a single Activity. Fragments


have their own events, layouts and complete life cycle. It provide flexibility and also
removed the limitation of single Activity on the screen at a time.

You might also like