You are on page 1of 43

SWE 218

Mobile
Programming
LESSON 8
Application
Components and
Lifecycle
Lifecycle in Android Architecture Components

The LifeCycle component is concerned with the Android


LifeCycle events of a component such as an Activity or a
Fragment, it has three main classes that we’ll deal with:
1. Lifecycle
2. Lifecycle Owner
3. Lifecycle Observer
Lifecycle in Android Architecture Components

1. Lifecycle

Lifecycle is a process that tells us about the Events


performed on an Activity/Fragment.
Lifecycle in Android Architecture Components
Lifecycle in Android Architecture Components
Lifecycle in Android Architecture Components

2. Lifecycle Owner

Every Activity has a Lifecycle. This Activity will be a


Lifecycle Owner(any activity or fragment can be called a
lifecycle owner).
Lifecycle in Android Architecture Components

3. Lifecycle Observer

Lifecycle Observer, which observes the activity and


keeps track of the lifecycle, and performs an action.
Java Application
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("lifecycle", "onCreate invoked");
}
@Override protected void onStart(){
super.onStart();
Log.e("lifecycle", "onStart invoked");
}
Java Application
@Override protected void onResume(){
super.onResume();
Log.e("lifecycle", "onResume invoked");
}
@Override protected void onPause(){
super.onPause();
Log.e("lifecycle", "onPause invoked");
}
@Override protected void onStop(){
super.onStop();
Log.e("lifecycle", "onStop invoked");
}
Java Application
@Override protected void onRestart(){
super.onRestart();
Log.e("lifecycle", "onRestart invoked");
}
@Override protected void onDestroy(){
super.onDestroy();
Log.e("lifecycle", "onDestroy invoked");
}
}
Output

You will not see any output on


the emulator or device. You need
to open logcat window.
Logcat
Logcat
Basic components
such as Activity,
Fragment, Service
Application Fundamentals
● Android apps can be written using Kotlin, the
Java programming language, and C++
languages.
● The Android SDK tools compile your code along
with any data and resource files into an APK or
an Android App Bundle.
App components
There are four types of app components:
● Activities
● Services
● Broadcast receivers
● Content providers
Question1
Write a simple Android program that displays "Hello,
World!" on the screen when the app is launched.
Answer 1
Answer 1
Question 2
Create an Android application that allows users to input
their name in a text field, and upon clicking a button,
displays a greeting message including their name in a
toast message.
Answer 2
// MainActivity.java
package com.example.greetingapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
Answer 2
public class MainActivity extends AppCompatActivity {
private EditText nameEditText;
private Button greetButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.nameEditText);
greetButton = findViewById(R.id.greetButton);
Answer 2
greetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString().trim();
if (!name.isEmpty()) {
String message = "Hello, " + name + "!";
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Please enter your name",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Answer 2
Question 3
Create an Android application for a calculator. The
application should include text fields where the user
can input two numbers, and buttons for addition,
subtraction, multiplication, and division operations.
When the user clicks on one of the operation
buttons, the result should be displayed in a text field.
Answer 3
Answer 3
Answer 3
Answer 3
Answer 3
Question 4
What is the purpose of an Intent in Android
programming?
Answer 4
An Intent in Android programming is used to facilitate communication between

components of the Android system, such as activities, services, broadcast

receivers, and content providers. It is a messaging object that can be used to

request an action from another app component or to pass data between

activities. Intents can be either explicit, specifying the target component by

name, or implicit, allowing the system to determine the appropriate component

to handle the request based on the provided action and data.


Question 5
Provide an example of how to create an explicit
Intent to start another activity in Android.
Answer 5
Question 6
Explain the concept of the Android Activity
lifecycle and its importance in developing robust
applications.
Answer 6
The Android Activity lifecycle consists of several key methods:

onCreate(): This method is called when the activity is first created.


onStart(): This method is called when the activity becomes visible to the user
but is not yet in the foreground.
onResume(): This method is called when the activity is about to become
visible and start interacting with the user.
onPause(): This method is called when the activity is partially obscured by
another activity or is about to be put in the background.
onStop(): This method is called when the activity is no longer visible to the
user.
onDestroy(): This method is called before the activity is destroyed.
Questions
8- Explain the difference between implicit and explicit intents in Android,
and provide a scenario where each type would be appropriate to use.
9- Explain the concept of Android Fragments and their advantages in
Android app development.

10- Explain the concept of Android services and discuss scenarios


where they are commonly used in Android app development.
11- Create an Android application that consists of two activities:
MainActivity and SecondActivity. MainActivity should contain a button,
and when this button is clicked, it should navigate to SecondActivity.
SecondActivity should display a TextView with the message "Welcome
to Second Activity!".
Answer 11
Answer 11
Answer 11
Resources
● https://www.geeksforgeeks.org/lifecycle-in-android-architecture-components/

● https://developer.android.com/guide/components/fundamentals

● https://developer.android.com/guide/topics/permissions/overview
Thanks for listening…

You might also like