Interview Preparation Kit for Senior Android Developers

Interview Preparation Kit for Senior Android Developers

A typical job interview is a discussion between two liars

Interviews are a headache in general. No matter where you stand in the experience scale, it comes with lot of self doubts, impostor syndromes, preparation burdens and what not. Especially when you are a experienced developer, somehow the companies are expecting us to be well equipped to solve all the world problems.

Not sure about how to solve all world problems, but I am preparing a questionnaire for Senior Android Developers with around 5 years of technical experience. This is for myself and might be useful to you.

Planning to split the article into multiple parts, grouped based on topics. I guess this approach would help us to practice and revise in specific to topics. Feel free to comment suggestions and correct my mistakes if you find any.


Android Core Questions

Explain activity life-cycle

Explain fragment life-cycle

What is context in Android?

  • In Android, Context is an important and fundamental class that provides access to various application-specific resources and information about the application environment.

  • It's an abstract class provided by the Android framework, and several classes within Android, such as Activities, Services, Application, and Broadcast Receivers, extend or implement the Context class.

  • It is used to access app specific resources, Files and database, Location Manager, Audio Manager, retrieve info about app package (version number, package name), view inflation, access theme, initiate service, broadcast, start Activity

What are the different types of Context in Android?

In Android, there are several types of Context available, each serving specific purposes or contexts within an application. These various Context types are differentiated based on their scope and the environment they provide access to:

  • Application Context (ApplicationContext):

    1. Obtained via getApplicationContext()

    2. Represents the application's entire life-cycle.

    3. Exists throughout the application's lifetime.

    4. Should be used when an object needs access to application-level resources or global application state. For example, retrieving resources, accessing the application's package information, or using system services.

    5. Be cautious not to store long-lived references to UI components (like Activities or Views) within the Application Context to prevent memory leaks.

  • Activity Context (ActivityContext):

    1. Obtained via this or getActivity().

    2. Represents the current state of an Activity.

    3. Exists as long as the activity is alive.

    4. Should be used when working with UI components (Views, Dialogs) or accessing resources that are tied to a particular activity, like inflating layouts or creating intents.

    5. Be careful not to pass the Activity Context beyond the lifetime of the activity (e.g., storing it in long-lived objects) to avoid memory leaks.

  • Service Context (ServiceContext):

    1. Obtained via this within a Service.

    2. Represents the context of a running Service.

    3. Exists as long as the service is running.

    4. Used within services for operations like interacting with system services, accessing resources, or managing service-related tasks.

  • Broadcast Receiver Context (ReceiverContext):

    1. Obtained via onReceive() method in a Broadcast Receiver.

    2. Represents the context of a broadcast being received.

    3. Exists during the execution of the onReceive() method.

    4. Used within Broadcast Receivers for handling broadcast intents, starting services, etc.

  • Content Provider Context (ContentProviderContext):

    1. Obtained via getContext() in a Content Provider.

    2. Represents the context of a Content Provider.

    3. Exists during the execution of Content Provider methods.

    4. Used within Content Providers for accessing and manipulating data through the Content Resolver.

What are Broadcast Receiver?

  • Broadcast Receivers in Android are components that facilitate communication between different parts of an application or between different applications.

  • They are particularly useful for responding to system-wide broadcast announcements made by the Android system or other applications. Battery Status Change, Network Changes, Push Notifications, Alarm Managers, Background Operations and scheduled tasks, Triggering UI components.

What are intents?

In Android, Intents are a fundamental part of the operating system that enables communication between components such as activities, services, broadcast receivers, and content providers. There are mainly three types of Intents

  • Implicit Intent

  • Explicit Intent

  • Pending Intent

Explain different types of Intents

Explicit Intent : Used to launch components within the same application by explicitly specifying the target component's name (i.e., its class name)

Intent intent = new Intent(CurrentActivity.this, TargetActivity.class); 
startActivity(intent);

Implicit Intent : Used to activate components (like activities, services, or broadcast receivers) without specifying the exact class name of the component to be invoked. (opening a link in browser)

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com")); 
startActivity(intent);

Pending Intent : Used in scenarios where an operation needs to be performed at a later time, such as sending a notification or scheduling an alarm, and the operation is triggered from a different context than where it will be executed. Pending Intents are a type of intent that allows you to pass a token representing a future execution of a specified intent.

Intent notificationIntent = new Intent(context, TargetActivity.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, 
                                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Is it possible to create activity without UI?

Yes it is possible. You simply need to remove UI initialisation in onCreate method.

What is ANR in android?

  • ANR occurs when the user interface thread (main thread or UI thread) of an Android application is blocked for an extended period, typically around 5 seconds or more, causing the app to become unresponsive to user interactions.

  • Common causes of ANR include: - Lengthy operations performed on the UI thread, such as long-running computations, network operations, or database queries.

  • Deadlocks or race conditions that lead to thread contention, causing the UI thread to halt.

What are crashes in Android?

  • Crashes in Android applications occur when the app encounters an unrecoverable error or exception during runtime, leading to the app's termination.

  • Null Pointer Exceptions (NPEs), ArrayIndexOutOfBoundsException, or other unchecked exceptions.

How to avoid ANR in Android

  • Offload Long-Running Operations from the UI Thread

  • Use Loaders or AsyncTasks Carefully

  • Avoid Network Operations on the Main Thread

  • Optimise Database Access

  • Use Handler Threads Wisely

  • Use Profiling Tools

What is the purposes of Service in Android?

Android Service is used to perform long running jobs off the UI thread. A typical long running tasks can be periodic downloading of data from internet, saving multiple records into database, perform file I/O, fetching your phone contacts list, etc. For such long running tasks, Service is used to avoid UI lags and makes user experience better.

What is the difference between bound and unbounded service?

  • Bound Service – Service which call indefinitely in between activity. An Android component may bind itself to a Service using bindservice (). A bound service would run as long as the other application components are bound to it. As soon as they unbind, the service destroys itself.

  • Unbound Service – Service which call at the life span of calling activity. In this case, an application component starts the service, and it would continue to run in the background, even if the original component that initiated it is destroyed. For instance, when started, a service would continue to play music in the background indefinitely.

How can we make the AlarmService run forever even after device reboot?

Once you start an AlarmService, it runs forever until your device restarts. Once your device restart, you have to start the service explicitly to run it forever again. You have to register BroadcastReceiver to handle boot event.

What are the key differences between a service and IntentService in Android?

ServiceIntentService
Service can be used in tasks with no UI, but shouldn’t be too long. If you need to perform long tasks, you must create a new thread with in ServiceIntentService can be used in long running tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents.
Service can be started using startService() methodIntentService can be started using startService() method and it triggers onHandleIntent() method.
Service can be triggered from any threadIntentService must be triggered from Main Thread
The Service may block the Main Thread of the application.The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.
You must call stopSelf() or stopService() to stop a service once your its job is done.IntentService stops itself when it finishes its job so you never have to call stopSelf()

What is the importance of the AndroidManifest.xml file in an Android application?

The AndroidManifest.xml file is a key component of an Android application. It is an XML file that provides essential information about the application to the Android operating system. Some of the important roles that the AndroidManifest.xml file in the Android SDK plays are:

  • Declaring the application's package name: The package name is a unique identifier for the application, and it is used to distinguish it from other applications installed on the device.

  • Declaring the application's components: The manifest file declares all the components of the application, including activities, services, broadcast receivers, and content providers. The Android OS uses this information to launch and manage these components.

  • Declaring the required permissions: The AndroidManifest.xml file lists all the permissions that the application requires to access system resources or other applications' data. When the application is installed, users are prompted to grant these permissions.

  • Declaring the application's minimum and target API levels: The manifest file specifies the application's minimum Android API level required to run and the target API level the application is built for.

  • Declaring the application's launch activity: The manifest file specifies which activity should be launched when the application is launched.

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 ViewModel, 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 AndroidManifest.xml 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.

What dialog boxes are supported on Android?

Android provides several types of dialog boxes that you can use to interact with users and display important information. Here are some commonly used dialog box types supported on Android:

  • AlertDialog: AlertDialog is a versatile dialog box that can display a title, message, and optional buttons. It is often used to prompt users for confirmation, display important information, or ask for user input.

  • ProgressDialog: ProgressDialog is a dialog box that shows the progress of a long-running operation. It typically displays a spinning progress indicator and an optional message to inform the user about the ongoing task.

  • DatePickerDialog: DatePickerDialog allows users to select a date from a calendar. It provides a calendar interface for choosing a specific date and returns the selected date to the application.

  • TimePickerDialog: TimePickerDialog allows users to select a time from a clock interface. It provides a clock-like interface for choosing hours and minutes and returns the selected time to the application.

  • BottomSheetDialog: BottomSheetDialog is a dialog that slides up from the bottom of the screen, partially covering the content. It is commonly used for displaying additional options or actions related to the current context.

  • Custom Dialogs: Android also allows you to create custom dialog boxes by extending the Dialog or DialogFragment class. This gives you full control over the appearance and behavior of the dialog, allowing you to design a dialog that suits your specific needs.

How do you handle background tasks in an Android application?

There are several ways to handle background tasks in an Android application, depending on the nature of the task and the requirements of the application. Here are some commonly used methods:

  • Service: A service is a component in the Android system that runs in the background and performs long-running operations, such as playing music, downloading large files, or performing network requests. A service can run indefinitely or be started and stopped on demand.

  • IntentService: An IntentService is a type of service that can handle multiple requests on a separate worker thread in a queue. It is suitable for handling long-running tasks in the background, such as downloading large files, and it automatically stops itself when the task is completed.

  • JobScheduler: The JobScheduler is a system service introduced in Android 5.0 Lollipop that allows you to schedule background tasks to run at specific times or conditions, such as when the device is idle or connected to a charger. It is suitable for performing periodic or recurring tasks, such as syncing data or sending notifications.

  • WorkManager: The WorkManager is a library introduced in Android Jetpack that provides a simple and flexible way to schedule and manage background tasks in your app. It automatically chooses the best implementation based on the device's API level, battery level, and network status.

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.

In summary, the main difference between a service and a broadcast receiver is that a service is used to perform long-running background tasks. In contrast, a broadcast receiver listens to system-wide broadcast events and performs actions in response to them. Services can be started and stopped on demand, while broadcast receivers always listen to events.


Resources that were helpful:
https://anywhere.epam.com/en/blog/advanced-android-interview-questions-answers
https://stacktips.com/articles/android-service-interview-questions


The above section is only for Android Core related questions. More questionnaire on topics like Kotlin, Async Programming, Jetpack Components, Test driven will be coming up soon. Feel free to comment down if I have missed out any important questions on this topic.