Lecture 4
Lecture 4
Activities
The Activities life cycle
Multiple Activities
Outline Bundel
Intent
Services
Toast
Activities
Activities in Android
Activity class is one of the very important parts of the Android Component.
Every activity contains the layout, which has a user interface to interact with
the user.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
How an Activity runs
Activity launched
onCreate()
App is running
Method Description
onCreate called when activity is first created.
onStart called when activity is becoming visible to the user.
// creating a intent
Intent intent = new Intent(this, SecondActivity.class);
// creating a bundle object
Bundle bundle = new Bundle();
// storing the string value in the bundle
bundle.putString("key1", "GFG :- Main Activity");
// passing the bundle into the intent
intent.putExtras(bundle);
// starting the intent
startActivity(intent);
To retrieve the data stored in the Bundle, by SecondActivity.
Examples:
● View details of a single item (for example, product in a shopping app)
● Create a new item (for example, new email)
● Show settings for an app
● Access services in other apps (for example, photo gallery or browse
documents)
Intents
What is Intent in Android?
Methods Description
Context.startActivity() This is to launch a new activity or get an existing activity to be action.
This is to start a new service or deliver instructions for an existing
Context.startService()
service.
Context.sendBroadcast() This is to deliver the message to broadcast receivers.
Types of Android Intents
There are two types of intents in android
1. Implicit Intent
● Provides generic action the app can perform
● Resolved using mapping of the data type and action to known components
● Allows any app that matches the criteria to handle the request
2. Explicit Intent
● Fulfills a request using a specific component
● Navigates internally to an Activity in your app
● Navigates to a specific third-party app or another app you've written
Types of Android Intents
1. Implicit Intent
Syntax:
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.geeksforgeeks.org/"));
startActivity(intent);
2. Explicit Intent
Syntax:
Intent i = new Intent(getApplicationContext(),
ActivityTwo.class);
startActivity(i);
Implicit Intent Example
Explicit Intent Example
Services
Services in Android
Services in Android are a special component that facilitates an application to
run in the background in order to perform long-running operation tasks.
The prime aim of a service is to ensure that the application remains active in
the background so that the user can operate multiple applications at the same
time.
Types of Android Services
Types of Android Services
1. Foreground Services:
Services that notify the user about its ongoing operations are termed as Foreground Services.
2. Background Services:
Background services do not require any user intervention. The process like schedule syncing of data
or storing of data fall under this service.
3. Bound Services:
Bound services perform their task as long as any application component is bound to it.
The Life Cycle of Android Services
Started Service (Unbounded Service):
A service will initiate when an application component calls the startService()
method.
Two option are available to stop the execution of service:
•By calling stopService() method,
•The service can stop itself by using stopSelf() method.
Bounded Service:
It can be treated as a server in a client-server interface.
onUnbind() The Android system invokes this method when all the clients
get disconnected from a particular service interface.
onRebind() Once all clients are disconnected from the particular interface of service and
there is a need to connect the service with new clients, the system calls this method.
Constants Description
public static final int
LENGTH_LONG displays for a long time