You are on page 1of 27

Unit 2

Using Activities, Fragments and Intents in Android


Content
 Activities & Activity Lifecycle
 Intents
 Fragments, Fragment Lifecycle
Activities

 An activity is the entry point for interacting with the user.


 It is a User Interface that contains activities.
 These can be one or more depending upon the App. (like websites)
 It starts when the application is launched.
 At least one activity is always present which is known as
MainActivity.
public class MainActivity extends Activity {
// processes
}
Activity Life Cycle
Activity Life Cycle

 onCreate()
 It is called when the activity is first created.
 Perform basic application startup logic that happens only once for the entire
life of the activity.
 After the onCreate() method finishes execution, the activity enters
the Started state and the system calls the onStart()
 onStart()
 This call makes the activity visible to the user as the app prepares for the
activity to enter the foreground and become interactive.
 It is followed by onResume() if the activity is invoked from the background.
 It is also invoked after onCreate() when the activity is first started.
Activity Life Cycle

 onResume()
 It is invoked when the activity starts interacting with the user.
 At this point, the activity is at the top of the activity stack, with a user
interacting with it.
 The app stays in this state until something happens to take focus away from
the app, such as the device receiving a phone call, the user navigating to
another activity, or the device screen turning off.
 When an interruptive event occurs, the activity enters the Paused state and the
system invokes the onPause() callback.
 If the activity returns to the Resumed state from the Paused state, the system
once again calls the onResume() method.
Activity Life Cycle

 onPause()
 There are several reasons why an activity might enter this state:
 An event that interrupts app execution, This is the most common case.
 In multi-window mode, only one app has focus at any time, and the system pauses all
the other apps.
 The opening of a new, semi-transparent activity, such as a dialog, pauses the activity it
covers. As long as the activity is partially visible but not in focus, it remains paused.
 It is invoked when an activity is going into the background but has not yet
been killed.
Activity Life Cycle

 onStop()
 It is invoked when the activity is not visible to the user.
 This can occur when a newly launched activity covers the entire screen.
 The system also calls onStop() when the activity finishes running and is about
to be terminated followed by onDestroy().
 It is followed by onRestart() when the activity is revoked from the
background.
 onDestroy()
 onDestroy() is called before the activity is destroyed.
 The activity is finishing due to the user completely dismissing the activity or
due to finish() being called on the activity.
 The system is temporarily destroying the activity due to a configuration
change ex. device rotation or multi-window mode.
 The onDestroy() callback releases all resources
Activity Life Cycle

 onRestart()
 It is invoked after the activity has been stopped and prior to its starting stage.
 Thus is always followed by onStart() when any activity is revived from
background to on-screen.
Activity Life Cycle
(src/com.example.helloworld/MainActivity.java)
Activity Life Cycle
Activity Life Cycle
Log messages in LogCat windows
 Run Android app

 to click lock screen button on the Android emulator

 again try to unlock your screen

 try to click Back button


Intents

 Intents, in general, are used for navigating among various activities within the same
application, or to another application as well.
 It is a messaging object which tells what kind of action to be performed.
 There are three fundamental use
 Starting an activity:
 to start a new instance of an Activity by passing an intent to startActivity()
 Starting a services:
 You can start a service to perform a one-time operation (such as downloading a file) by passing an Intent
to startService().
 Context.startService(Intent) or
 Context.bindService(Intent,BindServiceFlags,Executor,ServiceConnection) to communicate with a
background Service.
 Delivering a broadcast:
 The system delivers various broadcasts for system events, such as when the system boots up or the
device starts charging.
 Deliver a broadcast to other apps by passing an Intent to sendBroadcast() or sendOrderedBroadcast().
Intents

 Intent object can contain the following components:


 action: The general action to be performed, such
as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
 data: The data to operate on, such as a person record in the contacts database,
expressed as a Uri
 Basically two intents are there in android.
 Implicit Intents
 Explicit Intents
Implicit Intents

 Implicit Intent doesn’t specify the component.


 In such a case, intent provides information on available components
provided by the system that is to be invoked.
 Implicit intents are used without a class name, where Android will help
determine an appropriate Activity to handle the intent.
 For example, you may write the following code to view the webpage.

Intent intent=new Intent(Intent.ACTION_VIEW);


intent.setData(Uri.parse(“https://www.geeksforgeeks.org/”));
startActivity(intent);
Implicit Intents
Implicit Intents
Explicit Intent

 Explicit Intent specifies the component.


 In such a case, intent provides the external class to be invoked.
 Explicit Intent are used with class name, where Android navigate via
routes.

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


startActivity(i);
Fragment
 A Fragment represents a reusable portion of app's UI.
 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.
 A fragment defines and manages its own layout, has its own lifecycle,
and can handle its own input events.
 Fragments cannot live on their own they must be hosted by an activity
or another fragment.
 Android Fragment is the part of activity, it is also known as sub-activity.
 There can be more than one fragment in an activity.
 Using with fragment transaction. we can move one fragment to another
fragment.
Fragment Life Cycle
Fragment Life Cycle

 onAttach():
 The fragment instance is associated with an activity instance.
 The fragment and the activity is not fully initialized.
 Typically you get in this method a reference to the activity which uses the fragment for
further initialization work.
 onCreate():
 The system calls this method when creating the fragment.
 You should initialize essential components of the fragment that you want to retain
when the fragment is paused or stopped, then resumed.
 onCreateView() :
 The system calls this callback when it's time for the fragment to draw its user interface
for the first time.
 To draw a UI for your fragment, you must return a View component from this method
that is the root of your fragment's layout.
 You can return null if the fragment does not provide a UI.
Fragment Life Cycle

 onActivityCreated():
 The onActivityCreated() is called after the onCreateView() method when the host activity is
created.
 Activity and fragment instance have been created as well as the view hierarchy of the activity.

 onStart():
 The onStart() method is called once the fragment gets visible.

 onResume():
 Fragment becomes active.

 onPause():
 The system calls this method as the first indication that the user is leaving the fragment.
 This is usually where you should commit any changes that should be persisted beyond the
current user session.
Fragment Life Cycle

 onStop():
 Fragment going to be stopped by calling onStop().
 onDestroyView():
 Fragment view will destroy after call this method.
 onDestroy():
 onDestroy() called to do final clean up of the fragment's state but Not
guaranteed to be called by the Android platform.
 onDetach():
 The system executes this method to disassociate the fragment from its host
activity.
 It will call when your fragment Destroy(app crash or attach new fragment
with existing fragment)
References
 https://developer.android.com/
 https://www.oreilly.com/library/view/learning-android/9781449304881/c
h04.html
 https://www.javatpoint.com/android-tutorial
 https://www.geeksforgeeks.org/introduction-to-android-development/?ref
=lbp

You might also like