4.1-Android Fragments
4.1-Android Fragments
> 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
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:
Syntax:
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.
• Each fragment has its own life cycle methods that is affected by activity
life cycle because fragments are embedded in activity.
• 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.
• 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.
onActivityCreated() It is called when the fragment activity has been created and the fragment view
hierarchy instantiated.
•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