UNIT II - ActivityLifeCycle - UnderstandingActivities
UNIT II - ActivityLifeCycle - UnderstandingActivities
UNIT II - ActivityLifeCycle - UnderstandingActivities
Activities
1
They dictate the UI and handle the user interaction to the smart phone screen.
Services
2
They handle background processing associated with an application.
Broadcast Receivers
3
They handle communication between Android OS and applications.
Content Providers
4
They handle data and database management issues.
1. Activities
An activity represents a single screen with a user interface. Activity performs actions on the screen.
It is 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.
Example
An email application might have one activity that shows a list of new emails, another activity to
compose an email, and another activity for reading emails. If an application has more than one activity,
then one of them should be marked as the activity that is presented when the application is launched.
Example:
A service might play music in the background while the user is in a different application, or it might
fetch data over the network without blocking user interaction with an activity.
3.Broadcast Receivers
Broadcast Receivers simply respond to broadcast messages from other applications or from the system.
Example:
Applications can also initiate broadcasts to let other applications know that some data has been downloaded
to the device and is available for them to use, so this is broadcast receiver who will intercept this
communication and will initiate appropriate action.
4.Content Providers
A content provider component supplies data from one application to others on request.
Such requests are handled by the methods of the ContentResolver class.
The data may be stored in the file system, the database or somewhere else entirely.
A content provider is implemented as a subclass of ContentProvider class and must implement a
standard set of APIs that enable other applications to perform transactions.
ANDROIDMANIFEST.XML FILE
Every app project must have an AndroidManifest.xml file, with precisely that name, at the root of
the project source set.
The manifest file describes essential information about your app to the Android build tools, the Android
operating system, and Google Play.
Among many other things, the manifest file is required to declare the following:
1. The components of the app, including all activities, services, broadcast receivers, and
content providers.
Each component must define basic properties, such as the name of its Kotlin or
Java class.
It can also declare capabilities, such as which device configurations it can handle, and
intent filters that describe how the component can be started. Read more about app
components in a following section.
2. The permissions that the app needs in order to access protected parts of the system or
other apps.
It also declares any permissions that other apps must have if they want to
access content from this app.
For example, an app that needs to send SMS messages must have the following line in
the manifest:
<manifest ... >
<uses-permission android:name="android.permission.SEND_SMS"/>
...
</manifest>
3. The hardware and software features the app requires, which affects which devices can
install the app from Google Play.
Manifest elements
The following table provides some of the elements of Manifest (AndroidManifest.xml file).
Attribute
<!-- Beware that these values are overridden by the build.gradle file -->
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="26" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity" />
</application>
</manifest>
UNDERSTANDING ACTIVITIES
An activity represents a single screen with a user interface. Activity performs actions on the screen.
It is 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.
Activities include following details:
1) Lifecycle Of Activity
2) Linking Activities Using Intents
3) Applying Styles and Themes to an Activity
4) Hiding the Activity Title
1.Lifecycle Of Activity
An activity goes through a number of states. Activity lifecycle in Android manages the different states of
activity, such as when the activity starts or stops, etc. All of these states are managed by callback methods.
onCreate():-
Called when the activity is first created.
Example:
when we open the app it will call onCreate(), onStart(), and onResume() methods. These three methods
are initiated when you open an app.
onStart():-
Called when the activity becomes visible to the user
There should always be an onStart() callback after onCreate() finishes.
In the started state, users will be able to see the activity but they are not ready for user interaction.
onResume():-
Called when the activity starts interacting with the user
In this, activities will be in the foreground and ready for user interaction.
onPause():-
Called when the current activity is being paused and the previous activity is being resumed
It is the opposite of onResume().
onStop():-
Called when the activity is no longer visible to the user
onRestart():-
Called when the activity has been stopped and is restarting again
onDestroy():-
Called before the activity is destroyed by the system
This starts when the system needs to free memory or when finish() is called.
It is used for cleanup which is a very common activity.
By default, the activity created for you contains the onCreate() event.
Explicit Intent:
Explicit Intents are used to connect the application internally.
In Explicit we use the name of component which will be affected by Intent.
Example: If we know class name then we can navigate the app from One Activity to another activity
using Intent. In the similar way we can start a service to download a file in background process.
Explicit Intent work internally within an application to perform navigation and data transfer.
Implicit Intent:
In Implicit Intents we do need to specify the name of the component. We just specify the Action which
has to be performed and further this action is handled by the component of another application.
The basic example of implicit Intent is to open any web page
Changing the default theme to @android:style/Theme.Material, applies the Material Dark theme and
gives your application a darker look.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
To create a DialogFragment, create a class that extends DialogFragment and override onCreateDialog(),
as shown in the following example.
public class PurchaseConfirmationDialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
return new AlertDialog.Builder(requireContext())
.setMessage(getString(R.string.order_confirmation))
.setPositiveButton(getString(R.string.ok), (dialog, which) -> {} )
.create();
}
new PurchaseConfirmationDialogFragment().show(getChildFragmentManager(),
PurchaseConfirmationDialog.TAG);
5. Press Shift+F9 to debug the application on the Android emulator. Click the button to display
the dialog.Then Dialog box is displayed on the android emulator.