0% found this document useful (0 votes)
12 views27 pages

4.1-Android Fragments

The document provides an overview of Android mobile application development, focusing on Intents and Fragments. It explains the types of Intents (implicit and explicit), their functions, and how they interact with activities. Additionally, it details the Fragment lifecycle, their role in UI design, and how they can enhance app flexibility and user experience.

Uploaded by

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

4.1-Android Fragments

The document provides an overview of Android mobile application development, focusing on Intents and Fragments. It explains the types of Intents (implicit and explicit), their functions, and how they interact with activities. Additionally, it details the Fragment lifecycle, their role in UI design, and how they can enhance app flexibility and user experience.

Uploaded by

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

Welcome to

CSE3012 MOBILE APPLICATION DEVELOPMENT

G.GANESAN, B.E (CSE)., M.E (CSE)., M.S(IT).,(PhD)., MISTE.


Assistant Professor,
School of Computing Science and Engineering.
+91-9500234437
[email protected]
Content
Intent and Fragment

> Intent
> Types of Intent
> Fragment
> Fragment Lifecycle
Android Intent
Android Intent is a messaging object which passes between components like
services, content providers, activities, etc. Normally startActivity() method is
used for invoking any activity.
Some of the general functions of intent are:
• Start Service
• Launch Activity
• Display Web Page
• Display Contact List
• Message Broadcasting

Some Applications of Intents:


• Sending the user to another app
• Getting result from an Activity
• Allowing Other Apps to Start our Activity
Android Intent
Methods Description

This is to launch a new activity


Context.startActivity() or get an existing activity to be
action.
This is to start a new service
Context.startService() or deliver instructions for an
existing service.
This is to deliver the
Context.sendBroadcast() message to broadcast
receivers.
Types of Android Intent
Android Intent classified into Two
1. Implicit Intent 2. Explicit Intent

Implicit Intent
Implicit Intent does not specify the component.
It provides the information on available components provided by the system
that is to be invoked.
Ex: To view the web page.

Syntax:

Intent intent = new Intent(Intent.ACTION_VIEW);


intent.setData(Url.parse(“https://www.geeksforgeeks.org/”);
startActivity(intent);
Types of Android Intent
Types of Android Intent
Explicit Intent

The explicit intent used to specify any other component.


The targeted component is specified by explicit intent.
Only the specified target component will be invoked.

Syntax:

Intent intent = new Intent(getApplicationContext(), ActivityTwo.class);


startActivity(intent);
Types of Android Intent
Types of Android Intent
In the above exmple, there are two activities named
FirstActivity and SecondActivity.

When click on the “GO TO OTHER ACTIVITY” button in the FirstActivity, then it
move to the SecondActivity.

When click on the “GO TO HOME ACTIVITY” button in the SecondActivity, then it
move to the FirstActivity.
Android Fragments
• Android Fragment is the part of activity, it is also known as sub-activity.
There can be more than one fragment in an activity. Fragments represent
multiple screen inside one activity.

• Android fragment lifecycle is affected by activity lifecycle because


fragments are included in activity.

• Each fragment has its own life cycle methods that is affected by activity
life cycle because fragments are embedded in activity.

• The FragmentManager class is responsible to make interaction between


fragment objects.
Android Fragments
• In Android, the fragment is the part of Activity which represents a portion
of User Interface(UI) on the screen.

• It is the modular section of the android activity that is very helpful in


creating UI designs that are flexible in nature and auto-adjustable based
on the device screen size.

• The UI flexibility on all devices improves the user experience and


adaptability of the application.
Android Fragments
• Fragments are the modular section of activity design and these are used
to represent the behavior of user interface (UI) in an activity. By using
fragments we can create flexible UI designs that can be adjusted based on
the device screen size such as tablets, smartphones.

• We can build multi-pane UI by combining multiple fragments in a single


activity and we can reuse the same fragment in multiple activities. The
fragment has its own lifecycle call-backs and accepts its own input events.

• We can add or remove fragments in an activity while the activity is


running. In android, the fragment will act as a sub-activity and we can
reuse it in multiple activities.
Android Fragments
• Fragments are the modular section of activity design and these are used
to represent the behavior of user interface (UI) in an activity. By using
fragments we can create flexible UI designs that can be adjusted based on
the device screen size such as tablets, smartphones.

• We can build multi-pane UI by combining multiple fragments in a single


activity and we can reuse the same fragment in multiple activities. The
fragment has its own lifecycle call-backs and accepts its own input events.

• We can add or remove fragments in an activity while the activity is


running. In android, the fragment will act as a sub-activity and we can
reuse it in multiple activities.
Android Fragments
• Generally in android, the fragment must be included in an activity due to
that the fragment lifecycle will always be affected by the host activity life
cycle. In case if we pause an activity, all the fragments related to an
activity will also be stopped.

• In android, we can insert the fragment into activity layout by using


<fragment> element and by dividing the layout of activity into fragments,
we can modify the appearance of an app design at runtime. We can also
implement a fragment without having any user interface (UI).

• It’s an optional to use fragments into activity but by doing this it will
improve the flexibility of our app UI and make it easier to adjust our app
design based on the device size.
Android Fragments
• Generally in android, the fragment must be included in an activity due to
that the fragment lifecycle will always be affected by the host activity life
cycle. In case if we pause an activity, all the fragments related to an
activity will also be stopped.

• In android, we can insert the fragment into activity layout by using


<fragment> element and by dividing the layout of activity into fragments,
we can modify the appearance of an app design at runtime. We can also
implement a fragment without having any user interface (UI).

• It’s an optional to use fragments into activity but by doing this it will
improve the flexibility of our app UI and make it easier to adjust our app
design based on the device size.
Android Fragments
• Fragment interaction with the activity:
Types of Android Fragments
• Single Fragment: Display only one single view on the device screen. This
type of fragment is mostly used for mobile phones.

• List Fragment: This Fragment is used to display a list-view from which the
user can select the desired sub-activity. The menu drawer of apps like
Gmail is the best example of this kind of fragment.

• Fragment Transaction: This kind of fragments supports the transition


from one fragment to another at run time. Users can switch between
multiple fragments like switching tabs
Types of Android Fragments
• Example of defining a multiple fragments in single activity for the tablet
design to display the details of an item which we selected in the app, but
separated for mobile design.
Types of Android Fragments
• A Fragment represents a reusable portion of your app's UI. A fragment
defines and manages its own layout, has its own lifecycle, and can handle
its own input events.
Types of Android Fragments
• Fragments cannot live on their own--they must be hosted by an activity or
another fragment. The fragment’s view hierarchy becomes part of, or
attaches to, the host’s view hierarchy
Types of Android Fragments
Types of Android Fragments
Android Fragment Life Cycle
Android Fragment Life Cycle
Method Description
onAttach() It is called when the fragment has been associated with an activity.

onCreate() It is used to initialize the fragment.


onCreteView() It is used to create a view hierarchy associated with the fragment.

onActivityCreated() It is called when the fragment activity has been created and the fragment view
hierarchy instantiated.

onStart() It is used to make the fragment visible.


onResume() It is used to make the fragment visible in an activity.
onPause() It is called when fragment is no longer visible and it indicates that the user is leaving
the fragment.
onStop() It is called to stop the fragment using the onStop() method.
onDestoryView() The view hierarchy associated with the fragment is being removed after executing
this method.

onDestroy() It is called to perform a final clean up of the fragments state.


onDetach() It is called immediately after the fragment disassociated from the activity.
Android Fragment Life Cycle
Android Fragment Life Cycle

Fragments were added in in Honeycomb version of Android i.e API version


11. There are some primary classes related to Fragment’s are:

•1. FragmentActivity: The base class for all activities using compatibility
based Fragment (and loader) features.
•2. Fragment: The base class for all Fragment definitions
•3. FragmentManager: The class for interacting with Fragment objects inside
an activity
•4. FragmentTransaction: The class for performing an atomic set of Fragment
operations such as Replace or Add a Fragment.
Android Fragment Life Cycle

• Fragments were added in in Honeycomb version of Android i.e API


version 11. There are some primary classes related to Fragment’s are:

• 1. FragmentActivity: The base class for all activities using compatibility


based Fragment (and loader) features.
• 2. Fragment: The base class for all Fragment definitions
• 3. FragmentManager: The class for interacting with Fragment objects
inside an activity
• 4. FragmentTransaction: The class for performing an atomic set of
Fragment operations such as Replace or Add a Fragment.

You might also like