wisemonkeys logo
FeedNotificationProfileManage Forms
FeedNotificationSearchSign in
wisemonkeys logo

Blogs

Android Application Components and Activity Lifecycle

profile
Fahim
Oct 23, 2017
0 Likes
0 Discussions
451 Reads

Android Components

Main Components of Android Activities -> It provides a GUI(graphical user interface) for android application Activities are used by users to interact with the smartPhones. Services -> It is used to handle the background work with in an android application. Ex: WhatsApp New Message Arrive Notification Broadcast Receivers -> It helps you to communicate from Android Application to the Android hardware via Android OS Ex: Battery Low Warning Content Providers -> Its basically provides you the data from the other application Ex Contacts in WhatsApp

Activities

public class MainActivity extends Activity { 
}

or

public class MainActivity extends AppCompatActivity { 
}

Services

public class MyService extends Service {
}

Broadcast Receivers

A broadcast receiver is implemented as a subclass of BroadcastReceiverclass and each message is broadcaster as an Intent object.
public class MyReceiver  extends  BroadcastReceiver {
   public void onReceive(context,intent){}
}

Content Providers

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.
public class MyContentProvider extends  ContentProvider {
   public void onCreate(){}
}

Additional Components in Android

Fragments -> Part of an Activity like an Iframe in HTML Views -> UI elements that are drawn on-screen example text,button,image,etc. Layouts -> View hierarchies that control screen format and appearance of the views. Intents -> An Intent is a messaging object you can use to request an action from another app component.

Activity-lifecycle concepts

The Activity class defines the following call backs i.e. events. You don't need to implement all the callbacks methods. However, it's important that you understand each one and implement those that ensure your app behaves the way users expect. onCreate() -> This is the first method called when the activity is first created. onStart() -> It is method called when the activity becomes visible to the user. onResume() -> This method is called when the user starts interacting with the application. onPause() -> In this state activity does not receive user input and cannot execute any code.  onStop() -> This method is called when the activity is no longer visible. onDestroy() -> This method is called before the activity is destroyed by the system. onRestart() -> This method is called when the activity restarts after stopping it.  Example of Activity Lifecycle
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;

public class MainActivity extends Activity {
   String msg = "Android : ";
   
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Log.d(msg, "The onCreate() event");
   }

   @Override
   protected void onStart() {
      super.onStart();
 Log.d(msg, "The onStart() event"); 
   }

   @Override
   protected void onResume() {
      super.onResume();
 Log.d(msg, "The onResume() event"); 
//the activity has become visible
   }

   @Override
   protected void onPause() {
      super.onPause();
 Log.d(msg, "The onPause() event"); 
//activity is taking focus.
   }

   @Override
   protected void onStop() {
      super.onStop();
      Log.d(msg, "The onStop() event");
//the activity is no longer visible.
   }

   @Override
   public void onDestroy() {
      super.onDestroy();
       // the activity is destroyed.
      Log.d(msg, "The onDestroy() event");

   }
}

setContentView() Method is responsible for displaying the XML Layout in your android app, so always use it in onCreate() Method

setContentView(R.layout.activity_main); it will display Emulator window and you should see following log messages in LogCat window in Android studio −

The output somewhat will look like this in the text because here we are not dealing with the XML  Layout file

com.example.helloworld D/Android :: The onCreate() event

com.example.helloworld D/Android :: The onStart() event

com.example.helloworld D/Android :: The onResume() event


Comments ()


Sign in

Read Next

Is Social Media Marketing The Next Wave Of Digital Marketing?

Blog banner

LiquidPlanner

Blog banner

How to Manage Employees and Tasks in One System (Without Excel)

Blog banner

gis substation

Blog banner

security controls

Blog banner

Modern operating system

Blog banner

ONLINE NEWSROOMS

Blog banner

How Traditional Indian Sweets Make Every Festival More Special

Blog banner

The Real Reason Patola Outfits Come at a Premium Cost

Blog banner

What Are Electro-Forged Gratings? Benefits, Applications, and Why Industries Prefer Them

Blog banner

Password Generator - Lisp

Blog banner

What your Favorite colour says about You?

Blog banner

How International Schools Build Global-Minded Students through Curriculum & Activities

Blog banner

Memory heirchy

Blog banner

NIKE

Blog banner

SQL Injection practice on DVWA

Blog banner

Anomaly Detection in Behavioral Data Using Machine Learning

Blog banner

S-Tool : Steganography

Blog banner

Why Moms Are Choosing Customised School Accessories Over Generic Products?

Blog banner

Oracle Corporation

Blog banner

Incorporating Automation into Digital Forensics.

Blog banner

10 Types of Friends in every friend group

Blog banner

Embedded Operating System

Blog banner

The Memory Hierarchy

Blog banner

MODERN OPERATING SYSTEM

Blog banner

File Allocation Methods

Blog banner

Incident management in ITSM

Blog banner

Super Garlicky Tomato Soup with Smashed White Beans

Blog banner

'Positivity in life'

Blog banner

Deadlock and Starvation

Blog banner

What does the Australian summer have in store for your oral health?

Blog banner

10 Reasons to date your best friend

Blog banner

Cyber Forensic in the Banking sector

Blog banner

Unlocking the Secrets: Basic Operations of Computer Forensic Laboratories

Blog banner

Memory Management - operating system

Blog banner

Digital Life And Mental Health: A Psychological Perspective in the Modern World

Blog banner

Data Warehouse Bus Matrix

Blog banner

Short note on Microsoft office

Blog banner

Confidence Building Activities for the New School Year

Blog banner

Scheduling

Blog banner

Objectives and functions of Operating System...

Blog banner

BUSINESS MODELS OF E COMMERCE

Blog banner