Components of Android

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 36

Components of Android

Project
Main Building Blocks/Main components
behind any Android Application
Activity
Activity Lifecycle
Activity Lifecycle
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.


Intents- Action

• Think of Intents as a verb for an object


– A description of what needs to be done
– Example: VIEW, CALL, PLAY etc.
Types of Intent
Implicit Intent doesn’t specify the component.

Explicit Intent specifies the component.


Services
• Faceless components running in background

• They are of following types:


– Unbound Service
 It is a kind of service which runs in the background indefinitely, even if the
activity which started this service ends

– Bound Service
 It is a kind of service which runs till the lifespan of the activity which started
this service
Content Providers

• Enables sharing of data


across applications
– Example: address book,
photo gallery

• Provide uniform APIs for;


– Querying
– Delete
– Update
– Insert
Broadcast Receiver - Dormant Observer
Coding Productivity
Code Completion

 Code completion speeds up app development by reducing


typing errors and the need to look up class, method, and
variable names.

 The code editor provides basic completion, smart


completion, and statement completion.
Code Completion
 Code Completion helps us write code
quickly by automatically completing the
code using dynamic suggestion lists that
are generated based on what we just
typed.

 The basic code completion is the list of


suggestions that appears while we are
typing, as shown in the following
screenshot.
Code Completion

 If the list is not displayed, press Ctrl and the Spacebar to


open it.

 Type a statement,
press Ctrl + Shift + Enter (Cmd + Shift + Enter on OS X),
and notice how the closing punctuation is automatically
added (If you press these keys after typing).
Debugging Application
Debugging Application
 Android Studio provides a debugger that lets you do the following and
more;
 Select a device to debug your app on.
 Set breakpoints in your Java, Kotlin, and C/C++ code.
 Examine variables and evaluate expressions at runtime.

 Before you can begin debugging, do the following;


 Enable debugging on your device.
 If you're using the emulator, debugging is enabled by default. But for a
connected device, you need to enable debugging in the device
developer options.
Debug Window
Debug Window

 Execution and navigation toolbar


 Thread selector
 Evaluation and watch expression entry
 Stack display
 Variables pane
Meet Android Studio

https://developer.android.com/studio/intro#code_completion
Android Studio Features
 A flexible Gradle-based build system
 A fast and feature-rich emulator
 A unified environment where you can develop for all Android devices
 Live Edit to update composables in emulators and physical devices in real
time
 Code templates and GitHub integration to help you build common app
features and import sample code
 Extensive testing tools and frameworks
 Lint tools to catch performance, usability, version compatibility, and other
problems
 C++ and NDK support
 Built-in support for Google Cloud Platform, making it easy to integrate Google
Cloud Messaging and App Engine
Project Structure
 Each project in Android Studio contains one or more modules with source
code files and resource files. The types of modules include;
 Android app modules
 Library modules
 Google App Engine modules

 Each app module contains the following folders;


 manifests: Contains the AndroidManifest.xml file.
 java: Contains the Kotlin and Java source code files, including JUnit test code.
 res: Contains all non-code resources such as UI strings and bitmap images.

 To see the actual file structure of the project, select Project instead of
Android from the Project menu.
Gradle Build System
 Android Studio uses Gradle as the foundation of the build system,
with more Android-specific capabilities provided by the Android
Gradle plugin. This build system runs as an integrated tool from the
Android Studio menu and independently from the command line.

 You can use the features of the build system to do the following;
 Customize, configure, and extend the build process.
 Create multiple APKs for your app with different features, using the same
project and modules.
 Reuse code and resources across source sets.
 By employing the flexibility of Gradle, you can achieve all of this without
modifying your app's core source files.
Build variants

 The build system can help you create different versions of the same
app from a single project.

 This is useful when you have both a free version and a paid version of
your app or if you want to distribute multiple APKs for different
device configurations on Google Play.
Multiple APK support

 Multiple APK support lets you efficiently create multiple APKs based
on screen density or ABI.

 For example, you can create separate APKs of an app for the hdpi and
mdpi screen densities, while still considering them a single variant
and letting them share test APK, javac, dx, and ProGuard settings.
Resource Shrinking

 Resource shrinking in Android Studio automatically removes unused


resources from your packaged app and library dependencies.

 For example, if your app uses Google Play services to access Google
Drive functionality, and you are not currently using Google Sign-In,
then resource shrinking can remove the various drawable assets for
the SignInButton buttons.

 Note: Resource shrinking works in conjunction with code shrinking


tools, such as ProGuard.
Manage Dependencies
 Dependencies for your project are specified by name in the module-
level build script.

 Gradle finds dependencies and makes them available in your build.

 You can declare module dependencies, remote binary dependencies,


and local binary dependencies in your build.gradle.kts file.

 Android Studio configures projects to use the Maven Central


Repository by default. This configuration is included in the top-level
build file for the project.
Debug and Profile Tools

 Android Studio helps you debug and improve the


performance of your code, including inline debugging and
performance analysis tools.

 Inline debugging
 Use inline debugging to enhance your code walkthroughs in the
debugger view with inline verification of references, expressions,
and variable values.
 To enable inline debugging, in the Debug window, click Settings
and select Show Variable Values in Editor.
Performance Profilers

 Android Studio provides performance profilers so you can easily track


your app's memory and CPU usage, find de-allocated objects, locate
memory leaks, optimize graphics performance, and analyze network
requests.

 To use performance profilers, with your app running on a device or


emulator, open the Android Profiler by selecting View > Tool Windows
> Profiler.
Heap Dump

 When profiling memory usage in Android Studio, you can


simultaneously initiate garbage collection and dump the Java heap to
a heap snapshot in an Android-specific HPROF binary format file.

 The HPROF viewer displays classes, instances of each class, and a


reference tree to help you track memory usage and find memory
leaks.
Memory Profiler

 Use Memory Profiler to track memory allocation and watch where


objects are being allocated when you perform certain actions.

 These allocations help you optimize your app’s performance and


memory use by adjusting the method calls related to those actions.
Data File Access

 The Android SDK tools, such as Systrace and Logcat, generate performance
and debugging data for detailed app analysis.

 To view the available generated data files;


 Open the Captures tool window.
 In the list of the generated files, double-click a file to view the data.
 Right-click any HPROF files to convert them to the standard.
 Investigate your RAM usage file format.
Code Inspections
 Whenever you compile your program, Android Studio automatically runs configured
lint checks and other IDE inspections to help you easily identify and correct
problems with the structural quality of your code.

 The lint tool checks your Android project source files for potential bugs and
optimization improvements for correctness, security, performance, usability,
accessibility, and internationalization.
Annotations in Android Studio

 Android Studio supports annotations for variables, parameters, and


return values to help you catch bugs, such as null pointer exceptions
and resource type conflicts.

 The Android SDK Manager packages the Jetpack Annotations library in


the Android Support Repository for use with Android Studio.

 Android Studio validates the configured annotations during code


inspection.
Log messages

 When you build and run your app with Android Studio, you can view adb
output and device log messages in the Logcat window.
Sign in to your developer account
 Sign in to your developer account in Android Studio to access additional tools
that require authentication, such as Firebase. By signing in, you give those
tools permission to view and manage your data across Google services.
 Once you open a project in Android Studio, you can sign in to your developer
account or switch developer accounts as follows;
1. Click the profile icon at the end of the toolbar.
2. In the window that appears, do one of the following;
 If you're not yet signed in, click Sign In and allow Android Studio to access the
listed services.
 If you're already signed in, click Add Account to sign in with another Google
account.
 Alternatively, you can click Sign Out and repeat the previous steps to sign in to a
different account.

You might also like