0% found this document useful (0 votes)
30 views38 pages

Lecture 4

Uploaded by

happy baby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views38 pages

Lecture 4

Uploaded by

happy baby
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Lecture 4 ACTIVITIES

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.

Each screen your user interacts with is typically represented by an activity.

Every activity contains the layout, which has a user interface to interact with
the user.

An Android app consists of one or more screens or activities.


MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
How an Activity runs
Activity launched

onCreate()

App is running

Activity shut down


The Activity Lifecycle

Activity life cycle diagram:


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.


onCreate()
● Activity is created and other initialization work occurs
● You must implement this callback
● Inflate activity UI and perform other app startup logic
onStart()
● Activity becomes visible to the user
● Called after activity:
○ onCreate()
or
○ onRestart() if activity was previously stopped
onResume()
● Activity gains input focus:
○ User can interact with the activity
● Activity stays in resumed state until system triggers activity to be
paused
onPause()

● Activity has lost focus (not in foreground)


● Activity is still visible, but user is not actively interacting with it
● Counterpart to onResume()
onStop()
● Activity is no longer visible to the user
● Release resources that aren’t needed anymore
● Save any persistent state that the user is in the process of editing
so they don’t lose their work
onDestroy()
● Activity is about to be destroyed, which can be caused by:
○ Activity has finished or been dismissed by the user
○ Configuration change
● Perform any final cleanup of resources.
● Don’t rely on this method to save user data (do that earlier)
Save state
User expects UI state to stay the same after a config change or if the app is
terminated when in the background.

● Activity is destroyed and restarted, or app is terminated and activity is


started.
● Store user data needed to reconstruct app and activity Lifecycle changes:
○ Use Bundle provided by onSaveInstanceState().
○ onCreate() receives the Bundle as an argument when activity is created again.
Bundle
Android Bundles are generally used for passing data from one activity to
another.
The bundle is always used with Intent in Android.
Bundles can hold all types of values (int, String, boolean, char) and pass them to
the new activity.
Bundles values are sent and retrieved.
putInt(String key, int value), getInt(String key, int value)
Using the Bundle in the Android App
in the MainActivity.

// 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.

// getting the bundle back from the android


Bundle bundle = getIntent().getExtras();

// getting the string back


String title = bundle.getString("key1", "Default");
Multiple activities (screens) in an app
Sometimes app functionality may be separated into multiple screens.

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?

The intent is a messaging object which passes between

components like services, content providers, activities, etc.


some applications of Intents:
1.Sending the User to Another App
2.Getting a Result from an Activity
3.Allowing Other Apps to Start Your Activity
Some Important Method of Intent and their Description

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.

A service can run continuously in the background even if the application is


closed or the user switches to another application.

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.

A service is termed as bounded when an application component binds itself with


a service by calling bindService() method. To stop the execution of this service,
all the components must unbind themselves from the service by
using unbindService() method.
Fundamentals of Android Services
The following are some of the important methods of Android Services:
Methods Description
onStartCommand() The Android service calls this method when a component(eg: activity)
requests to start a service using startService().

onBind() This method is invoked


whenever an application component calls the bindService() method in order to
bind itself with a service.

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.

onCreate() This method is necessary to perform


a one-time-set-up.

onDestroy() The system invokes this method


just before the service destroys as a final clean up call.
Toasts for Android App
A toast provides a simple popup message that is displayed on the current activity
UI screen.
Constants of Toast class

Constants Description
public static final int
LENGTH_LONG displays for a long time

public static final int


LENGTH_SHORT displays for a short time
Methods of Toast class
Methods Description
public static Toast makeText(Context makes the toast message consist of text
context, CharSequence text, int duration) and time duration

public void show() displays a toast message

public void setMargin (float changes the horizontal and vertical


horizontalMargin, float verticalMargin) differences
Thank
you

You might also like