Android Preparation Notes
Android Preparation Notes
“When a coroutine hits a suspend function like delay(), it doesn’t block the thread. Instead, the coroutine suspends — which means the Kotlin compiler has already transformed the suspend function into a state machine at compile time.
So when the suspend function is done — like after 100ms in delay(100) — the system resumes the coroutine using that Continuation, jumping straight to the next label.”
basic what happens when a function suspends in coroutine https://docs.google.com/document/d/1I6jgTA6mqAmxpfxg6Bp9oyMJeCudEf1vZrL0njd1bgQ/edit?usp=sharing
OnCreate(): Called when the activity is first created. This is where you should do all of your
normal static set-ups: create views, bind data to lists, etc., as it is called only once.
OnCreate is called once, when activity is created; hence all init are done here OnStart(): Called when the activity first becomes visible. It is called every time an activity
basic What is the difference b/w onCreate() and onStart()? OnResume is called multiple times when ever the screen become visible to user returns from the background. Can be called multiple times
basic When only onDestroy is called for an activity without onPause() and onStop()? When onDestroy() is called in onCreate funtion When you directly call finish() in onCreate() lifecycle method
As onCreate() of an Activity is called only once, this is the point where most initialization
should go. It is inefficient to set the content in onResume() or onStart() (which are called
basic Why do we need to call setContentView() in onCreate() of Activity class? because onCreate() is called only once. multiple times) as the setContentView() is a heavy operation.
1. onSavedInstanceState()- Use to save state(with running info) when activity goes to
background(pausedor stopped)
2. onRestoreSavedInstanceState()- Use to recover saved state when activity resumes
3. Used to save the state of the UI, such as the text in a text field or the position of a scroll
basic Use of onSavedInstanceState() and onRestoreInstanceState( to store UI state on cofig change or when activity is paused bar
1. Serializable :Standard Java interface that allows objects to be serialized to and
Serializable is Java ' interface; Parcelable is android specific feature, it is more efficient deserialized from a stream of bytes
medium What is the difference between Serializable and Parcelable? Which is the best approach in Android? 2. Parcelable: Android specific framework, fast, smaller, efficient
1. Standard: New instance is created every time an activity is launched
2. SingleTop: activity already exists at the top of the current task then no new activity
created and if it not then new activity is created - usecase - search activity
3. SingleTask: At a time only one instance of activity will exist. A->B->C->D will become A-
>B when b is created again and data will be routed using onNewintent - ex
https://stackoverflow.com/questions/33616136/what-are-the-real-world-use-cases-of-the-launchmodes-in-android mainDashboard, music/video player
4. Single Instance - new activity will always be created on new task
must Android Launch Modes used to define how a new instance of an activity should be launched or how it should interact with existing instances of the same activity
https://stackoverflow.com/questions/34487702/what-are-the-use-cases-when-we-should-prefer-a-broadcast-receiver-aidl-ibinder
A language which is used to communicate between two process/apps that doesn't know
Low Priority ADIL https://www.youtube.com/watch?v=JxmozhBzzcg anything about each other as processes dont sahre memory in android
onAttach()-> is called after Fragment is associated with its Activity Gets a reference to the
Activity object which can be used as Context
onCreate()-> Don't use onCreate to access View hierarchy because Activity's
onCreatemay/may not be finished. Create background threads here for long running
operations.
onCreateView()-> You are expected to return a View Hierarchy for your fragment
onActivityCreated()-> Called after Activity onCreate has completed execution Use this
method to access/modify UI elements
onResume()
onPause()
onDestroyView()-> Called after the Fragment View Hierarchy is no longer accessible
onDestroy()-> Called after fragment is not used. It still exists as a Java object attachedto
https://developer.android.com/guide/fragments/lifecycle the Activity
High Priority https://www.youtube.com/watch?v=vk7VKUFOlbY onDetach()-> Fragment is not tied to the Activity and does not have a View hierarchy
1. SharedVM - recommneded when persistant data need to be shared. It is an ideal choice
when you need to share data between multiple fragments or between fragments and their
host activity.
2. Fragment Result Api - For one time result that be saved in bundle
In this setFragmentResultListener() is used on receiver's end andsetFragmentResult is
basic How to communicate between two fragments https://developer.android.com/guide/fragments/communicate used on sender's end
A Service is an application component that can perform long-running operations in the
background. It does not provide a user interface
Types:
1. Foreground- Noticeable to the user, ex audio app. Must display a notification. Using
WorkManager is prefered to do these types of operations
2. Background - performs an operation that isn't directly noticed by the user. ex backup
data operation. After API level 26 it is rescrticted to use background service when app is
not in foreground.
Main types:
1. Foreground
1. Started service: Ideal for long running task that do not require commnunication b/w activity and sewrvices - Large
2. Background:After
file upload api level 26 restricted certain features
2. Bound Services: This service allow communication betweenactivity and services 3. Bound service - Can bound user interface to make the user interact. ex- music player
basic Service- Usecases,type all detail 3. Foreground services: Used during long running task useage is indiucated using a persistant notification
A work manager is a software component that is responsible for scheduling and executing
tasks, typically in a background thread.
A work manager allows tasks to be executed asynchronously, which can improve the
performance, reliability, and responsiveness of an application.
--> Guaranteed, constraint-aware execution, Respectful of system background restrictions,
https://medium.com/androiddevelopers/introducing-workmanager-2083bcfc4712 Backwards compatible with or without Google Play Services, Queryable, Chainable,
High Priority WorkManager - Detail Opportunistic
FragmentPagerAdapter: Each fragment visited by the user will be stored in the memory but
the view will be destroyed. When the page is revisited, then the view will be created not the
instance of the fragment.
FragmentStatePagerAdapter: Here, the fragment instance will be destroyed when it is not
Medium What is the difference between FragmentPagerAdapter vs FragmentStatePagerAdapter? visible to the user, except the saved state of the fragment.
The ViewHolder pattern is a technique for improving the performance of a list view by
recycling the views used to display the list items, reducing the amount of memory and
medium What is the ViewHolder pattern? Why should we use it? https://stackoverflow.com/questions/21501316/what-is-the-benefit-of-viewholder-pattern-in-android processing power needed to display the list.
Dialogs are created and displayed using the Dialog class, while Dialog Fragments are
created and displayed using the DialogFragment class.
Dialogs are created and displayed directly within an activity, while Dialog Fragments are
created and displayed within a fragment manager associated with an activity.
Dialogs are not added to the fragment back stack, while Dialog Fragments are added to the
basic What the difference between Dialog and Dialog Fragment https://stackoverflow.com/questions/7977392/android-dialogfragment-vs-dialog fragment back stack and can be managed along with other fragments in the activity.
Intent: works like a messaging component between fundamental components of Android
Use case: starting an activity, service or broadcast
Types:
1. Implicit - destination not known. Like opening a url
2. Explicit - the destination is known - open activity B from activity A
3. Pending - When you want to open some application components like
Activity/Service/BroadcastReceiver at later time or after some specified time interval you
have to send PendingIntent
must What is intent, types 4. Sticky Intent
Apps can send or receive broadcast messages from the Android system and other Android
apps.
When a broadcast is sent, the system automatically routes broadcasts to apps that have
subscribed to receive that particular type of broadcast.
basic What is BroadcastReceiver? https://developer.android.com/guide/components/broadcasts Ex System broadcasts are sent to all apps that are subscribed to receive the event.
An intent filter is an expression in an app's manifest file that specifies the type of intents
that the component would like to receive
There are three Intent characteristics you can filter on: the action, data, and categories
Ex: if you want our app to have the capability to open a file say an Image from a file
manager or other app, we can use an intent filter in the activity that can render the image.
Code ex:
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<data android:scheme="file" android:mimeType="image/*"/>
<category android:name="android.intent.category.DEFAULT" />
medium What is the function of an IntentFilter https://developer.android.com/reference/android/content/IntentFilter </intent-filter>
An intent that is used with sticky broadcast, is called as sticky intent. This intent will stick
with android system for future broadcast receiver requests.
works in conjunction with sendStickyBroadcast() method. So when android system send
sticky broadcast for any change in battery status the Intent stick around even after the
BroadcastReceiver has completed it’s work. So at later point of time anyone can retrieve
medium What is a Sticky Intent? that Intent for getting the battery status result through registerReceiver() method
1. A component (such as an activity or service) creates an intent with the desired action
and data, and sends the intent using the sendBroadcast method on the Context class.
2. The Android system receives the broadcast intent and sends it to all registered
broadcast receivers that are interested in the intent's action.
3. Each broadcast receiver receives the intent and can extract the data from the intent and
basic Describe how broadcasts and intents work to be able to pass messages around your app? https://stackoverflow.com/questions/7276537/using-a-broadcast-intent-broadcast-receiver-to-send-messages-from-a-service-to-a
perform any necessary actions.
In service the process run on the same thread but intent service the process work on a new
basic Service vs IntentService. worker thread
using startActivityForResult(). we can define genric contacts or Custom contract. Custom
Contact has two functions -
1. createIntent() -> which takes content and inout a a parameter.
Low Priority how new intent result Api works official doc 2. parseIntent() -> get out , parse and return the data needed back to activity
Low Priority How can two distinct Android apps interact? https://developer.android.com/training/basics/intents
Low Priority Is it possible to run an Android app in multiple processes? How? https://stackoverflow.com/questions/6567768/how-can-an-android-application-have-more-than-one-process
Low Priority What is AIDL? Enumerate the steps in creating a bounded service through AIDL. https://developer.android.com/guide/components/aidl
basic What can you use for background processing in Android? https://developer.android.com/guide/background
A content provider is an application component used as a central database for all the apps
https://developer.android.com/guide/topics/providers/content-provider-basics the device to perform operations:
basic What is a ContentProvider and what is it typically used for? https://developer.android.com/guide/topics/providers/content-providers 1. ex contacts in android
How to run parallel tasks in Java or Android, and get callback when all complete? https://amitshekhar.me/blog/long-running-tasks-in-parallel-with-kotlin-flow
Too much load on the UI thread:
1. Input dispatching timeout - no response within 5 sec to user inputs
What is ANR? How can the ANR be prevented? https://developer.android.com/topic/performance/vitals/anr 2. Unable to start service/forground service in 5 secs
Explain Looper, Handler and HandlerThread.
How do you handle bitmaps in Android as it takes too much memory https://developer.android.com/topic/performance/graphics/manage-memory
Memory leak:-Failure to release unused objects from the memory
Flow keywords-> Flow Builder, Operator, Collector, flowOn, dispatchers, Operators such as filter, map, zip,
flatMapConcat, retry, debounce, distinctUntilChanged, flatMapLatest, Terminal operatorsCold Flow vs Hot Flow: Cold
Flow vs Hot Flow, StateFlow, SharedFlow, callbackFlow, channelFlow https://amitshekhar.me/blog/flow-api-in-kotlin
Android Development Best Practices. https://amitshekhar.me/blog/android-development-best-practices
What are the metrics that you should measure continuously while android application development? https://amitshekhar.me/blog/android-app-performance-metrics
How to avoid API keys from check-in into VCS? https://amitshekhar.me/blog/how-does-the-kotlin-multiplatform-work
How to use Memory Heap Dumps data?
How to secure the API keys used in an app?
How to increase the Notification delivery rate?
Can you create transparent activity in Android?
How to show local Notification at an exact time? we can use the alarm manager to start a notification
Const val vs val const val mut be known at compile time. Can only be string or preimitive type. It can only be init in global scope or inside objects
nline keyword a function whole body is passed around
lateinit vs lazy Both allow deferred initialisarion, lateinit is more like differed var and lazy as difered val
How to handle dependencies or abstraction in multi module
basic Multi Module benefits and why use it
Recycled view pools allow multiple RecyclerViews to share a common pool of scrap views.
This can be useful if you have multiple RecyclerViews with adapters that use the same
view types, for example if you have several data sets with the same kinds of item views
basic What is recyclerViewPool displayed by a ViewPager.
OncreateViewHolder
OnbindViewHolder
OnViewAttachedToWindow
Medium What is viewHolderLifecycler https://medium.com/android-news/recyclerview-optimisations-a4b141dd433d OnViewDetachToWindow
What i lint
Leu cache implantation
Systems design question for downloading file(s)
basic what is clean architecture
Kotlin specific
There are two cases:
What will extension function be when converted to kotlin 1. When function is in global scope: new clss is created with same name but kt extra
1.Chatted with interviewer about various technical questions such as http protocol, Android activity life cycles,
general background thread processes. Wrote script to do for modifying a string using bubble sort algorithm.
2. Program to find out rectangular overlap
3. you have to find minimum number of the given range that is not present in the array
4.How can you arrange an array of 0s and 1s without using an additional variable/memory?
5. Write code for 3 sorting algorithms
6. was asked to add and remove elements from a heap
7. 2d array island problem
No Idea inmobi