4. Activities
• An activity is a class that is considered as an entry point for users that
represents a single screen.
• Each activity is independent of one another.
• Each process state – Call Back
5. • To implement an activity, extend the Activity class in your subclass:
public class MainActivity extends Activity {
//code
}
6.
7. onCreate( )-This is the first callback that is fired when the system first creates
an activity. Android Studio itself calls this method in the java file
New Empty
Activity's Java File
8. onStart( )
• The system invokes this callback when the activity enters the Started
state. This method is called once the onCreate( ) callback has
finished.
• The onStart( ) call’s work is to makes the activity visible to the
user as the app prepares for the activity to come in the foreground
and start interacting with the user.
• This method is where the app initializes the code that maintains the
User Interface.
9. onResume( )
• When the activity enters the Resumed state, the system calls the
onResume( ) callback and the activity is now in the foreground.
• The Resumed state is where the activity can interact with the user.
• Any activity continues to stay in this state until focus is taken away
from the app by some other actions performed.
protected void onResume()
{
super.onResume();
}
10. onPause( )
• The onPause( ) method is called when the user is leaving the activity.
• It does not always indicate that the activity is being destroyed, but it tells that
the activity is now no longer in the foreground.
• Whenever a user leaves any activity it first enters into the Paused state and the
onPause( ) method is called, then it enters the Stopped state and the onStop( )
method is called.
• Running in background
• You can use the onPause( ) method to release the system resources if any in use
by your application, such as GPS or Camera as the activity is in the background
and the user no longer needs them.
11. Example
protected void onPause() {
super.onPause();
//When activity enters Paused state the onPause() method is called.
//Checking is the app is using camera.
if (camera != null) {
//If camera is in use, release the camera resource.
camera.release();
//Setting the camera variable to null.
camera = null;
}
}
12. onStop( )
• The activity is in Stopped state when it is no longer visible to the user
and the system invokes the onStop( ) callback. The activity can go
Stopped for various reasons, for example:
• If the user navigates to a new activity and that new activity completely covers
the screen.
• If the user minimizes the application and it is now in the background.
• When the activity is about to terminate, the system enters Stopped state,
invokes onStop( ) callback once the onPause( ) callback is finished.
13. onDestroy( )
• The system invokes the onDestroy( ) callback before the activity is
about to be destroyed. Following are the reasons for onDestroy to be
called:
• The activity is finished due to the user completely closing the activity or due
to the finish( ) method being called inside the activity.
• the system is temporarily destroying the activity due to a configuration
change such as device rotation or multi-window mode.
14. Services
• A service is a component that runs in the background to perform long-running
operations.
• A service does not provide a user interface.
• it acts as an invisible worker of our application. It keeps updating data sources and
activities.
• It also broadcasts intents and performs tasks when applications are not active.
• An example of service is we can surf the internet or use any other application while
listening to music.
• To execute services, extend the Services class in your sub-class:
public class MyService extends Services {
//code
}
15.
16. Content Providers
• Content Provider is a component that allows applications to share
data among multiple applications.
• It hides the details of the database and can be used to read and write
private data of the application which is not shared.
• To implement this, extend ContentProvider in your subclass:
public class Provider_Name extendsContentProvider {
public void onCreate(){}
}
17.
18.
19. Broadcast Receiver
• Broadcast Receiver is a component that responds to broadcast messages
from another application or the same system.
• It can also deliver broadcasts to applications that are not running.
• For example – notify the user that the battery is low.
• To implement this, extend BroadcastReceiver to your receiver:
public class MyReccceiver extends BroadcastReceiver {
public void onReceive(context,intent)
{
//code
}
}
20. • Broadcast Receivers simply respond to broadcast messages from other
applications or from the system itself. These messages are sometime
called events or intents.
• let other applications know that some data has been downloaded to the
device and is available for them to use
21.
22.
23. Build an intent
• An Intent is an object that provides runtime binding between separate
components, such as two activities. The Intent represents an app’s intent to
do something.
• Intent intent = new Intent(First_activity.class , Second_activity.class);
• An intent is to perform an action on the screen. It is mostly used to start
activity, send broadcast receiver,start services and send message between
two activities
24.
25. Steps
• Create the Activities
• Add the Activities to the app’s Manifest
• Create an Intent referencing the Activity class you want to switch to
• Call the startActivity(Intent) method to switch to the Activity
• Create a back button on the new Activity and call the finish()
method on an Activity when the back button is pressed
26. How to send data from one activity to second
activity
• For this, Intent will start and following methods will run:
•
putExtra() method is use for send the data, data in key value
pair key is variable name and value can be Int, String, Float etc.
• getStringExtra() method is for get the data(key) which is send by
above method. according the data type of value there are other
methods like getIntExtra(), getFloatExtra()