Posts

Showing posts from May, 2021

Android Fragments

In Android, there are different types of devices having different sizes and different hardware designs. To introduce modularity and reusability of the activity’s UI, Fragments were introduced. Fragments are called as reusable UI components who have their own layout, lifecycle and can handle its own input events. They very similar to how Activity functions in Android. Major difference is that Fragment provides the required flexibility to update the UI design in an easier way.  Unlike Activity,  Fragments  can easily be added, removed and can easily enter or exit provided screen space. Let's have an example News Application. Assume the app has 2 screens: 1. List of News items 2. Details of each new item (open when an news items is clicked in #1 screen) Generally, having 2 activities for each of these screens will be the first possible approach. For phone devices  (where width is smaller) this approach can be considered and implemented. But for the devices with larger w...

AndroidManifest

Image
One of the most important components of your project. Stays in the root directory of the project source. This is used to describe essential information about the app to Android Build Tools, Android OS and Google Play. Most important things for which AndroidManifest is used: To declare app’s package name: Used by Android build tools to map code entities and to map it with applicationId defined in build.gradle To declare different app components: Include all activities, services, broadcast receivers, and content providers To declare app permissions: Helps in accessing specific restricted by System or other apps. To declare hardware and software features: Used to decide which device can app be installed into Package name and application ID Example <?xml version="1.0" encoding="utf-8"?>        <manifest            xmlns:android="http://schemas.android.com/apk/res/android"           package="co...

Android Intent Filters

As the name suggests it helps filtering the right app component which Android System is searching for based on the properties set in the intent object. Declared in the AndroidManifest file of the project as a child tag to component tags like <activity>, <activity-alias>, <service>, <receiver>, <provider>. Syntax: <intent-filter> … </intent-filter> Attributes android:icon: A drawable resource. Specifies an icon for the activity. Can be used as a user facing icon. android:label: A user-readable label for the parent component. android:priority: An integer value. Activities with highest priority are considered potential targets for the new intent request. Receivers are provided to handle the request in the order of the priority set. android:order: An integer value, introduced in API 28 i.e does not have impact for lower than 28 API level. Tells the system which filter should be processed in case multiple filters are matched. Child tags <action...

Android Intent

Intent is a messaging object which is used to start a specific app component. Used to start an app component: Intent is a way using which one component of an app (e.g activity) can start another app component (e.g activity, service, etc). Used as a messaging object: Apart from helping start a new app component it is also used to share the information from one app component to another. Internals Stored at android.content.Intent path of the Android library. It has several methods like startActivity, startActivityForResult, startService, bindService, sendBroadcast, sendOrderedBroadcast, etc. Intent Structure Intent is a class which can store different properties and information which will be used to resolve the type of app component and to share the information with the new component. Component name Set using setComponent(), setClass(), setClassName(), or with the Intent constructor. Set name of the component to start. This is optional but not setting it will convert the intent into “im...

Activity Lifecycle

Image
To let Android System interact with Application different methods are defined by the system which communicate different states of the activity to the application. Those methods/states are called as Lifecycle Callbacks. Activity can go into different combination of these states. In the code each callback is used to understand the state in which the activity is in and accordingly perform different operations. Let’s discuss different activity callbacks and what each of them represent in real life scenarios. You will find each section explained with the 3 labels. Let us see what each label represents: Called: It will tell you when is this method called. Implement: What should be implemented in this method. State: What is the state of activity from user's perspective.                         onCreate() Called: This is the first callback received in Activity which is called as soon as the system creates the Activity. Implement: Used ...

Android Activities

Image
What is Activity? Activity is the fundamental concept in Android development. It is an entry point which provides a surface to let users interact with the application. Every activity is independent of each other though they work together to form a cohesive user experience. Activity creates a window on which you can place your UI which the user can interact to. These are just glue classes that provide an interface for letting apps communicate with the system and vice-versa. So the result is that the OS can destroy these components anytime based on different reasons. Activity are called as UI-based class. Things Activity provides: To let the system know to keep the process running which is hosting the activity (app in a whole). Keeps track of previous processes which the user can return to. Helps in handling process kill scenarios, so that previous state can be restored. Helps apps to implement user flow between each other, and systems to coordinate this flow. Things to consider while de...

Android Application Fundamentals

Image
Unlike desktop apps which have a single entry point and run as a single, monolithic process. Android applications have much more complex structure. They are built using multiple app components. The complete android code is packaged into a single file called APK (an Android Package). Languages supported: Java, Kotlin and C++. Important features of an Android app: Android OS is based on the Linux operating system, a multi-user OS, where every app is a different user represented by a unique Linux user ID. (Exception: It is possible to have apps with the same user Linux user ID i.e everything between these apps can be shared) Each app has its own linux “process” which is hosted on its private Virtual machine VM. System controls the “process” i.e starting and stopping of the process. To access any special device data, the app has to request it from the system. Building blocks of Android app AndroidManifest file Core Framework components - Activities, Services, Broadcast receivers, Content P...