Android Development Interview Questions & Answers

1. What is Android?

Answer:
Android is an open-source mobile operating system based on the Linux kernel and primarily designed for touchscreen mobile devices, such as smartphones, tablets, and wearables. Initially developed by Android Inc., it was later acquired by Google in 2005. Android allows developers to create applications in Java, Kotlin, or other programming languages. Android apps can run on a wide range of devices, from smartphones and tablets to TVs, wearables, and even cars.

2. What are the main components of an Android application?

Answer:
The core components of an Android application are:

  • Activity: Represents a single screen in the application (e.g., a login screen or a home screen).

  • Service: A component that runs in the background to perform long-running operations like downloading files or playing music.

  • Broadcast Receiver: A component that listens for system-wide events (like a battery low notification or a system shutdown) and responds accordingly.

  • Content Provider: Manages shared data between applications. For example, it can allow your app to access data stored in another app, like contacts or media files.

Each of these components can interact with each other to create a functional application.

3. What is the difference between explicit and implicit intents?

Answer:

  • Explicit Intent: It specifies the exact component (such as an Activity or Service) to start. For example, if you want to open a specific activity within your app, you would use an explicit intent.

Intent intent = new Intent(this, MyActivity.class);
startActivity(intent);
  • Implicit Intent: It does not specify the target component. Instead, it allows the system to find an appropriate component based on the action (e.g., opening a website, sending an email).

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

4. What is the Android lifecycle?

Answer:
Android applications go through various stages as the user interacts with them. Understanding the Activity Lifecycle is essential for handling configuration changes (like screen rotations) and managing resources effectively. The lifecycle states include:

  • onCreate(): Called when the activity is first created. Used for initializing the activity (e.g., setting up UI elements).

  • onStart(): Called when the activity becomes visible to the user.

  • onResume(): Called when the activity starts interacting with the user.

  • onPause(): Called when the system is about to start resuming another activity (used to pause ongoing tasks).

  • onStop(): Called when the activity is no longer visible.

  • onDestroy(): Called before the activity is destroyed.

Managing the activity lifecycle correctly is crucial for performance and preventing memory leaks.

5. What is a Fragment?

Answer:
A Fragment is a modular section of an activity that can be combined with other fragments to create multi-pane layouts or more flexible UIs. A fragment has its own lifecycle, but it is always tied to an activity. It is often used on tablet-sized screens to allow for flexible layouts, such as having multiple fragments displayed simultaneously.

A fragment can:

  • Have its own UI (e.g., a button, text field, etc.).

  • Be dynamically added or removed from an activity.

  • Communicate with the host activity and other fragments.

6. What is a RecyclerView?

Answer:
RecyclerView is an advanced version of ListView in Android, designed to handle large sets of data efficiently. It uses a ViewHolder pattern to reduce the overhead of inflating views and improves performance by reusing views that are no longer visible. RecyclerView is often used for displaying scrollable lists of items (e.g., messages in a chat app or a list of contacts).

It consists of:

  • Adapter: Binds the data to the views.

  • LayoutManager: Defines the layout of the list (e.g., linear, grid).

  • ViewHolder: Caches the view to avoid re-inflating views.

7. What is ANR (Application Not Responding)?

Answer:
ANR occurs when the application does not respond to user input within a certain time frame (typically 5 seconds for UI threads and 10 seconds for background threads). This usually happens when long-running operations are being performed on the main thread, which is responsible for handling UI updates. Common causes include:

  • Heavy operations like database queries, network calls, or disk I/O on the main thread.

  • Improper synchronization leading to deadlocks.

Solution: Always perform long-running operations in background threads using AsyncTask, Thread, Handler, or Coroutines (in Kotlin).

8. What is the difference between Serializable and Parcelable?

Answer:

  • Serializable: A standard Java interface that allows you to convert an object into a byte stream and store it or transfer it (e.g., across activities). It is relatively slow because it uses reflection.

  • Parcelable: A more efficient, Android-specific interface for serializing data. It is optimized for use in Android because it allows the app to control how data is serialized, and it is faster than Serializable.

Recommendation: Use Parcelable for better performance, especially for large data sets.

9. What is the use of AsyncTask?

Answer:
AsyncTask was a utility class designed for running background tasks in Android without blocking the UI thread. It allows developers to run tasks like downloading data or processing in the background while updating the UI. It has three main methods:

  • doInBackground(): Runs in the background thread.

  • onPreExecute(): Runs on the UI thread before background work starts (used for setup).

  • onPostExecute(): Runs on the UI thread after background work finishes (used to update the UI).

However, AsyncTask is now deprecated in newer Android versions, and Coroutines or ExecutorService are recommended instead for background work.

10. What is a Service in Android?

Answer:
A Service is a component that runs in the background to perform long-running operations without interacting with the user interface. Services can run independently from the main UI thread and continue even when the user switches to another app. There are two types of services:

  • Started Service: A service that is started by an application component (e.g., an activity) and runs indefinitely until explicitly stopped.

  • Bound Service: A service that allows other components to bind to it and interact with it (e.g., a music player service).

11. What are SharedPreferences in Android?

Answer:
SharedPreferences is a key-value storage mechanism that allows you to store small amounts of data (such as user preferences or settings) persistently. The data is stored in a file in XML format. This is commonly used for storing things like user preferences, flags, or configuration values.

Example of usage:

SharedPreferences preferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("username", "JohnDoe");
editor.apply();

12. What are Content Providers used for?

Answer:
Content Providers are used to share data between different applications. They provide a standard interface to access data, such as contacts, media, or custom app data. Apps can read, write, or update data through a Content Resolver. Examples include the ContactsProvider and MediaStore.

Common use cases:

  • Accessing shared data like contacts or media files.

  • Sharing app data between different apps in a structured way.

13. What is the role of AndroidManifest.xml?

Answer:
The AndroidManifest.xml file is a critical configuration file for an Android application. It contains essential information about the app, such as:

  • Declaring app components (e.g., activities, services, content providers, broadcast receivers).

  • Defining app permissions (e.g., network access, location access).

  • Specifying hardware and software features required (e.g., camera, GPS).

  • Declaring app themes, version, and other metadata.

It is essential for the system to know how to launch and interact with the app.

14. What is the difference between dp, sp, px in Android?

Answer:

  • dp (Density-independent Pixels): A unit of measurement that allows for consistent layout across devices with different screen densities. It is the recommended unit for designing UIs.

  • sp (Scale-independent Pixels): Similar to dp, but it also considers the user’s font size preferences (important for accessibility).

  • px (Pixels): Physical screen pixels. It is not recommended to use px for UI layouts as it doesn’t adapt to different screen densities.

Recommendation: Use dp for layout dimensions and sp for text sizes.

15. What is the use of Retrofit in Android?

Answer:
Retrofit is a type-safe HTTP client for Android used to interact with RESTful APIs. It abstracts the process of making network calls and parsing responses, making it easier to handle HTTP requests and responses. Retrofit works by converting API responses into Java objects using converters like Gson or Moshi.

Example:

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.example.com/")
    .addConverterFactory(GsonConverterFactory.create())
    .build();

ApiService apiService = retrofit.create(ApiService.class);

Retrofit handles network requests asynchronously by default, improving performance and user experience.

16. What are Handler and Looper in Android?

Answer:

  • Handler: A Handler allows you to send and process messages or runnable tasks on a specific thread’s message queue. It is commonly used to update the UI thread from a background thread.

  • Looper: A Looper is responsible for looping over a message queue and processing messages one by one. Every thread has its own looper, but the UI thread (main thread) has a default looper.

Use Case: You can use a Handler to send messages or run tasks from background threads to the UI thread using the looper of the main thread.

17. What is ViewModel in Android Architecture?

Answer:
ViewModel is part of the Android Architecture Components and is designed to store and manage UI-related data in a lifecycle-conscious way. A ViewModel is used to store data needed by an Activity or Fragment, and it survives configuration changes like screen rotations. This helps prevent the need for data to be recreated every time the Activity is recreated, which enhances performance and user experience.

18. What is LiveData in Android?

Answer:
LiveData is a lifecycle-aware data holder class in Android. It is part of the Android Architecture Components and is used for observing changes in data. A key feature is that it only updates active observers (such as an Activity or Fragment), ensuring that UI updates are performed only when the observer is in a valid lifecycle state (e.g., resumed).

It is commonly used in conjunction with ViewModel to provide a robust solution for handling UI updates.

19. What is the role of Coroutines in Android development?

Answer:
Coroutines are a modern, Kotlin-based approach for handling asynchronous programming in Android. They allow you to write asynchronous code sequentially, making it more readable and maintainable compared to callbacks or AsyncTask. Coroutines are lighter, more efficient, and provide better control over concurrency.

Example:

CoroutineScope(Dispatchers.IO).launch {
    val data = fetchDataFromNetwork()
    withContext(Dispatchers.Main) {
        updateUI(data)
    }
}

20. What are Android’s different types of storage options?

Answer: Android provides several storage options:

  • Internal Storage: Data is stored within the app’s private sandbox, and other apps cannot access it.

  • External Storage: Data is stored on external storage devices (e.g., SD cards). Requires user permission, and data can be shared across apps.

  • SQLite Database: A lightweight, relational database for storing structured data.

  • SharedPreferences: Stores key-value pairs for simple, small amounts of data (e.g., app preferences).

  • Cloud Storage: Remote storage options like Firebase or Google Drive allow apps to store data in the cloud.