0% found this document useful (0 votes)
2 views10 pages

Android Interview Questions

The document covers various aspects of Android development, including the lifecycle of activities and fragments, the differences between activities and fragments, data binding, intents, handling configuration changes, and the Android application lifecycle. It also discusses components like services and broadcast receivers, communication between fragments, design patterns like ViewHolder and Singleton, and modern programming concepts such as coroutines and dependency injection. Additionally, it addresses performance optimization for Android Maps applications and real-time location updates.

Uploaded by

bjthjyothi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Android Interview Questions

The document covers various aspects of Android development, including the lifecycle of activities and fragments, the differences between activities and fragments, data binding, intents, handling configuration changes, and the Android application lifecycle. It also discusses components like services and broadcast receivers, communication between fragments, design patterns like ViewHolder and Singleton, and modern programming concepts such as coroutines and dependency injection. Additionally, it addresses performance optimization for Android Maps applications and real-time location updates.

Uploaded by

bjthjyothi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

When I am in my activity onResume() & get a phone call what will be


the lifecycle of the fragment inside my activity —
There will be no change in lifecycle if a call comes.
If call is lifted then OnPause() & OnStop() is called.
After the call screen is closed then OnStart() & OnResume() is called.

2. What is the difference between a fragment and an activity in Android?


In Android, an activity represents a single screen with a user interface with
which the user can interact, while a fragment represents a portion of an
activity's user interface or behavior that can be reused across multiple activities.

The key difference between an activity and a fragment is that an activity can
exist independently and be launched independently, while an activity must host
a fragment. A single activity can host multiple fragments, and each fragment
can be added, removed, or replaced at Android runtime based on the user's
interaction.

Another difference is that an activity has a life cycle of its own, while a
fragment depends on its hosting activity's life cycle. When the hosting activity
is destroyed, all its associated fragments are also destroyed.

3. How do you implement data binding in an Android application?

Data binding is a powerful feature in Android mobile application software


development that allows you to bind UI components in your layout files to
data sources in your app, such as a View Model or model object. This can
help simplify your code, reduce boilerplate, and make your app more
efficient.
4. What is the difference between an implicit and an explicit intent in
Android?

In Android, intents are a way to communicate between different components of


an application and between different applications. They are used to request an
action from the system, such as opening a specific activity, sending a message,
or sharing data.
There are two types of intent in Android: implicit intent and explicit intent.
An explicit intent specifies the component to be invoked by providing the name
of the class to be instantiated.
On the other hand, an implicit intent does not specify the exact component to be
invoked. Instead, it specifies an action to be performed and the data involved in
the action. Implicit intents are typically used to perform system-level actions,
such as sending an email, making a phone call, or browsing the web.

5. How do you handle configuration changes like screen rotations in an


Android application?

When a configuration change occurs in an Android application, such as a


screen rotation or a language change, the Android system destroys and
recreates the activity. This process can lead to loss of data and affect the user
experience. To handle configuration changes in an Android platform, you
can use one or more of the following techniques:

Save and restore instance state: The onSaveInstanceState() method of the


activity is called before the activity is destroyed. You can use this method to
save the current state of the activity, such as the values of UI components or
any other important data, in a bundle object. The bundle is then passed to the
onCreate() method of the activity when it is recreated, and you can use the
values from the bundle to restore the state of the activity.
Use the ViewModel: ViewModel is a component of the Android
Architecture Components library that helps you to manage UI-related data in
a life-cycle-conscious way. You can store the data in the View Model, and it
will survive configuration changes because it is not tied to the activity's life
cycle.
Handle configuration changes manually: You can prevent the activity
from being destroyed and recreated on configuration changes by specifying
the android:configChanges attribute in the activity tag of the
[Link] file. This attribute tells the system to handle the
configuration change manually, and the activity's onConfigurationChanged()
method will be called instead of the recreated activity.

In summary, handling configuration changes in an Android application


requires preserving and restoring important data to prevent data loss and
ensure a good user experience. To achieve this, you can use techniques such
as saving and restoring instance state, using ViewModel, using retained
fragments, or handling configuration changes manually.

6. What is the Android application life cycle, and how do you handle each
stage?

onCreate(): This is the first method that is called when the application is
launched. This method is used to initialize the application and set up any required
resources.
onStart(): This method is called after onCreate() and is used to prepare the
application to be visible to the user.
onResume(): This method is called when the application is about to start
interacting with the user. This is where the application is in the foreground and
actively running.
onPause(): This method is called when the application is about to move into the
background. This is a good place to save any unsaved data or release any resources
that are no longer needed.
onStop(): This method is called when the application is no longer visible to the
user. This is a good place to release any resources that are no longer needed.
onDestroy(): This method is called when the application is destroyed. This is a
good place to release any resources not released during the onStop() or onPause()
methods.
onRestart(): This method is called when the application has been stopped before
being started again.

7. How do you implement localization in an Android application?

8. What is the difference between a service and a broadcast receiver in


Android?

A service and a broadcast receiver are components in the Android operating


system that perform background tasks but differ in their functions and how
they are used.

A service in the Android system runs in the background and performs long-
running operations, such as downloading files, playing music, or performing
network requests. A service can run indefinitely or be started and stopped on
demand. Services can be started in two ways: started services and bound
services. A started service runs in the background until it completes its task
or is stopped, while a bound service runs only as long as a client is bound to
it.

On the other hand, a broadcast receiver is a component in the Android


system that listens for system-wide broadcast events, such as the battery
level changing, a phone call being received, or a new SMS message being
received. When an event occurs, the broadcast receiver is notified and can
perform some action in response, such as displaying a notification or starting
a service.

9. How would you communicate between Two Fragments?


All communication between fragments occurs either through a shared
ViewModel or through the related Activity. Direct communication between
two Fragments should not be allowed.

To communicate across fragments, it is advised to make a shared


ViewModel instance. Both fragments have access to the ViewModel via the
Activity that contains them. If the data is exposed using LiveData, the other
fragment will be pushed with the updated data as long as it is monitoring the
LiveData from the ViewModel. The fragments can update data within the
ViewModel.

[Link] is a ViewHolder Pattern? Why should We use it?


When the adapter calls the getView() function, it also calls the
findViewById() method. As a result of the mobile CPU having to perform
such heavy lifting, the application’s performance suffers and battery life
degrades. FindViewById shouldn’t be used repeatedly. Instead, utilize the
ViewHolder design pattern.

The efficiency of the application improves because a ViewHolder stores a


reference to the id of the view resource, eliminating the need to “find” it
again later.

1O. Explain Dependency Injection.


When a large number of objects have to be created that are dependent on a
large number of other objects in a project, it becomes difficult as the project
grows larger. With this increasing code base, good external support is
required to keep track of everything. That is one of the scenarios in which
we employ a Dependency Framework.

If we have two activities, Activity A and Activity B. Both require an object


Downloader, which in turn requires the request. The request will now be
dependent on Executor and HTTPClient. Dependency Injection is based on
the concept of Inversion of Control, which states that a class’s dependencies
should come from outside. In other words, no class should instantiate
another class. Instead, instances should be obtained from a configuration
class.
Dependency Framework is used for the following reasons:

It facilitates the management of complex dependencies.


It simplifies unit testing by allowing us to pass all external dependencies
so that we can easily use mocked objects.
It easily manages the object’s scope (lifecycle).
This is why we need to use a Dependency Injection Framework in
Android, such as Dagger.

[Link] is Singleton Class in Android?


The Singleton Pattern is a software design pattern that restricts the
instantiation of a class to just “one” instance. It is used in Android
Applications when an item needs to be created just once and used across the
board. The main reason for this is that repeatedly creating these objects, uses
up system resources. The identical object should therefore only be created
once and used repeatedly. It is utilized in situations where we only require a
single instance of the class, such as with network services, databases, etc.
We, therefore, create a singleton class to implement the Singleton pattern in
our project or product. In this article, we’ll look at how to create singleton
classes in Java and Kotlin.

12. What is Coroutine on Android?


Coroutines are lightweight threads that allow us to do synchronous and
asynchronous programming with ease. We can build the main safety and replace
callbacks with coroutines without blocking the main thread. The concurrency
design pattern of the coroutine allows for code simplification and asynchronous
execution, which can offer very high levels of concurrency with a very
small overhead.
[Link] is Kotlin?
Kotlin is a modern programming language developed by JetBrains. It is
statically typed and runs on the Java Virtual Machine (JVM). The Kotlin
language is designed to be interoperable with Java, so you can use Kotlin
code alongside Java code seamlessly. Aside from Android app development,
it is also used for server-side and web development.

[Link] is Kotlin different from Java?

Kotlin and Java both programming languages that run on the Java Virtual
Machine (JVM). but Kotlin is designed to be more concise and expressive,
reducing unnecessary code. One notable feature is its built-in null safety,
helping to avoid common programming errors related to null values. Kotlin also
introduces modern language features like extension functions and coroutines,
offering developers more flexibility and improved productivity compared to
Java.

[Link] is the difference between val and var in Kotlin?

In Kotlin, `val` and `var` are used to declare variables, but they have different
characteristics:

 `val` is used to declare read-only (immutable) variables. Once assigned, the


value of a `val` cannot be changed.
 `var` is used to declare mutable variables. The value of a `var` can be
reassigned multiple times.

[Link] is the Elvis operator in Kotlin?

The Elvis operator (?:) is a shorthand notation in Kotlin that provides a default
value when accessing a nullable object. It is useful in scenarios where you want to
assign a default value if a nullable object is null.
[Link] is a lambda expression in Kotlin?

A lambda expression in Kotlin is a way to define a function-like construct without


explicitly declaring a function. It allows you to create a block of code that can be
passed around as an argument or stored in a variable.

18. What is the use of the lateinit modifier in Kotlin?

The lateinit modifier in Kotlin is used to declare properties that will be assigned a
value later, but not at the time of declaration. It is specifically used with mutable
properties of non-null types.

[Link] is ANR and How can it be Prevented in Android?

ANR stands for Application Not Responding. An ANR will occur if


you’re running a process on the UI thread which takes an extended time,
usually around 5 seconds. During this point, the GUI (Graphical User
Interface) will lock up which can end in anything the user presses won’t
be actioned. After the 5 seconds approx. has occurred, if the thread still
hasn’t recovered then an ANR dialogue box is shown informing the user
that the appliance isn’t responding and can give the user the choice to
either wait, in the hope that the app will eventually recover, or to force
close the app.

Stop doing heavy tasks on the main thread. Instead, use worker threads
such as IntentService, AsyncTask Handler, or another Thread simply.
Detecting where ANRs happen is straightforward if it’s a permanent block
(deadlock acquiring some locks for instance), but harder if it’s just a
short-lived delay. First, re-evaluate your code and appearance for
vulnerable spots and long-running operations.
[Link] time permissions in android?

21. Can you describe the main differences between the Google Maps SDK and
the Google Maps JavaScript API?

22. How do you handle location permissions while using Android Maps?
Specifically, how do you request and manage the location permissions at
runtime?

23. How can you optimize the performance of an Android Maps


application when displaying a large number of markers and overlays?
What strategies can you employ to minimize lag?

To optimize Android Maps performance with numerous markers and


overlays, implement the following strategies:

1. Cluster markers: Group nearby markers into a single marker to reduce


clutter and improve rendering speed. Use libraries like Android Maps Utils
for clustering.

2. Load data on demand: Fetch and display only visible markers/overlays


within the current viewport. Update as the user pans or zooms.

3. Optimize overlay rendering: Minimize custom drawing by using built-in


shapes and styles. For complex overlays, use hardware acceleration and
cache bitmaps.

4. Throttle map events: Limit event handling frequency (e.g., camera


movement) to avoid excessive processing during rapid interactions.

5. Utilize background threads: Perform heavy computations and data loading


asynchronously to prevent UI thread blocking.

6. Simplify geometries: Reduce the complexity of polygons and polylines by


removing unnecessary points or using simplified versions.
24. How do you handle real-time location updates in Android Maps?
Can you describe a scenario where real-time location updates might be
required?

To handle real-time location updates in Android Maps, use the


FusedLocationProviderClient and LocationCallback. First, request necessary
permissions (ACCESS_FINE_LOCATION or
ACCESS_COARSE_LOCATION). Then, create an instance of
FusedLocationProviderClient and set up a LocationRequest object with
desired interval, fastest interval, and priority. Next, implement a
LocationCallback to receive location updates. Finally, call
requestLocationUpdates() on the FusedLocationProviderClient, passing the
LocationRequest and LocationCallback.

25. Can you explain the role of location providers in Android Maps and
how to choose the best provider for specific use cases?

Location providers in Android Maps determine the device’s location using


various sources like GPS, Wi-Fi, and cellular networks. They play a crucial
role in providing accurate location data for apps requiring geolocation
services.

To choose the best provider for specific use cases, consider factors such as
accuracy, power consumption, and response time. For high-precision
requirements, GPS is ideal but consumes more power. Network-based
providers (Wi-Fi and cellular) offer lower accuracy but consume less power
and work indoors.

Use the Criteria class to define desired attributes and pass it to the
LocationManager’s getBestProvider() method. This returns the most suitable
provider based on the criteria.

You might also like