MOBILE APPLICATION DEVELOPMENT
(102046712)
PROF. JINAL BHAGAT
UNIT – 2 ACTIVITIES AND FRAGMENTS
Android Activity Lifecycle methods
Let's see the 7 lifecycle methods of android activity.
Method Description
onCreate - called when activity is first created.
onStart - called when activity is becoming visible to the user.
onResume - called when activity will start interacting with the user.
onPause - called when activity is not visible to the user.
onStop - called when activity is no longer visible to the user.
onRestart - called after your activity is stopped, prior to start.
onDestroy - called before the activity is destroyed.
ACTIVITY IN ANDROID
An activity in Android is a specific combination of XML files and
JAVA files. It is basically a container that contains the design as
well as coding stuff. XML files provide the design of the screen
and JAVA files deal with all coding stuff like handles, what is
happening, design files, etc.
FRAGMENTS IN ANDROID
Fragment class in Android is used to build dynamic User Interfaces. Fragment
should be used within the Activity.
A greatest advantage of fragments is that it simplifies the task of creating UI
for multiple screen sizes. A activity can contain any number of fragments.
Multiple fragments can be combined in a single activity to build a multi-pane
VI and reuse a fragment in multiple activities.
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.
ANDROID FRAGMENTS LIFECYCLE
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 IMPLICIT INTENT EXAMPLE
No. Method Description
1) onAttach(Activity) it is called only once when it is attached with activity.
2) onCreate(Bundle) It is used to initialize the fragment.
3) onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns view hierarchy.
4) onActivityCreated(Bundle) It is invoked after the completion of onCreate() method.
5) onViewStateRestored(Bundle) It provides information to the fragment that all the saved state of fragment view hierarchy has been restored.
6) onStart() makes the fragment visible.
7) onResume() makes the fragment interactive.
8) onPause() is called when fragment is no longer interactive.
9) onStop() is called when fragment is no longer visible.
10) onDestroyView() allows the fragment to clean up resources.
11) onDestroy() allows the fragment to do final clean up of fragment state.
12) onDetach() It is called immediately prior to the fragment no longer being associated with its activity.
ANDROID INTENT
Android Intent is the message that is passed between
components such as activities, content providers, broadcast Android intents are mainly used to:
receivers, services etc.
Start the service
It is generally used with startActivity() method to invoke
activity, broadcast receivers etc. Launch an activity
The dictionary meaning of intent is intention or purpose. So, Display a web page
it can be described as the intention to do action. Display a list of contacts
Intent allows you to interact with components from same Broadcast a message
application as well as components contributed by different
Dial a phone call etc.
applications for ex. An activity can start external activity for
taking pictures.
TYPES OF ANDROID INTENTS
There are two types of intents in android: implicit and
explicit.
1) Implicit Intent
2) Explicit Intent
Implicit Intent doesn't specifiy the component. In such case, Explicit Intent specifies the component. In such
intent provides information of available components case, intent provides the external class to be
provided by the system that is to be invoked. invoked.
For example, you may write the following code to view the Intent i = new Intent(getApplicationContext(), Acti
webpage.
vityTwo.class);
Intent intent=new Intent(Intent.ACTION_VIEW);
startActivity(i);
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);