Android Intermediate Questions
1. What is the life cycle of Android activity?
OnCreate(): It is called when activity is created. Using this, the views are created and data is collected from bundles.
OnStart(): It is called if the activity is becoming visible to the user. It may be succeeded by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.
OnResume(): It is called when the activity will start an interaction with the user.
OnPause(): This is called when the activity is moving to the background but hasn’t been killed yet.
OnStop(): This is called when an activity is no longer visible to the user.
OnDestroy(): This is called when the activity is finished or destroyed.
OnRestart(): This is called after the activity has been stopped, prior to it being started again.
2. Explain Sensors in Android.
Android-based devices have a collection of built-in sensors in them, which measure certain parameters like motion, orientation, and many more through their high accuracy. The sensors can be both hardware and software based on nature. There are three prominent categories of sensors in Android devices. They are:
Position Sensor: It is used for measuring the physical position of the Android device. This has orientation sensors and magnetometers.
Motion Sensors: These sensors consist of gravity, rotational activity, and acceleration sensors which measure the rotation of the device or the acceleration, etc.
Environmental Sensor: It includes sensors that measure temperature, humidity, pressure, and other environmental factors.
3. Explain the dialog boxes supported on Android.
Android supports four dialog boxes. They are:
AlertDialog:
The AlertDialog supports 0-3 buttons, along with a list of selectable items such as checkboxes and radio buttons.
It is used when you want to ask the user about taking a decision between yes or no in response to any particular action taken by the user, by remaining in the same activity and without changing the screen.
DatePickerDialog:
It is used for selecting the date by the user.
TimePickerDialog:
Used for selecting the time by the user.
ProgressDialog:
It is an extension of the AlertDialog and is used to display a progress bar. It also supports the addition of buttons.
This class was deprecated in API level 26 because it prevents the user from interacting with the application. Instead of this class, we can use a progress indicator such as ProgressBar, which can be embedded in the user interface of your application.
4. What is AndroidManifest.xml file and why do you need this?
The AndroidManifest.xml file contains information regarding the application that the Android system must know before the codes can be executed.
This file is essential in every Android application.
It is declared in the root directory.
This file performs several tasks such as:
Providing a unique name to the java package.
Describing various components of the application such as activity, services, and many more.
Defining the classes which will implement these components.
5. What is an intent?
An intent is a messaging object that is used to request an action from other components of an application. It can also be used to launch an activity, send SMS, send an email, display a web page, etc.
It shows notification messages to the user from within an Android-enabled device. It alerts the user of a particular state that occurred. There are two types of intents in Android:
Implicit Intent- Used to invoke the system components.
Explicit Intent- Used to invoke the activity class.
6. Mention the difference between class, file and activity in Android?
The difference between them is as follows:
Class is a compiled form of a .java file that Android uses to produce an executable .apk file.
A file is a block of arbitrary information or resources used for storing information. It can be of any file type.
Activity is a single screen that represents GUI(Graphical User Interface) with which users can interact in order to do something like dial the phone, view email, etc.
7. What is a Toast? Write its syntax.
Toast is a message that pops up on the screen. It is used to display the message regarding the status of the operation initiated by the user and covers only the expanse of space required for the message while the user’s recent activity remains visible and interactive.
Toast notification automatically fades in and out and it does not accept interaction events.
Syntax:
Toast.makeText(ProjectActivity.this, "Your message here", Toast.LENGTH_LONG).show();
8. What is context?
The context in Android is the context of the current state of the application or object. The context comes with services like giving access to databases and preferences, resolving resources, and more.
There are two types of context. They are:
Activity context
This activity context is attached to the lifecycle of an activity.
The activity context can be used when you are passing the context in the scope of an activity or you need the context whose lifecycle is attached to the context of the activity.
Application context:
This application context is attached to the lifecycle of an application.
The application context should be used where you need a context whose lifecycle is separate from the current context or when you are passing a context beyond the scope of activity.
9. Explain the difference between Implicit and Explicit Intent.
The difference between the implicit and explicit Intents are given below:
Explicit Intent:
An Explicit Intent is where you inform the system about which activity should handle this intent. Here target component is defined directly in the intent.
For example,
Intent i = new Intent(this, Activitytwo.class); #ActivityTwo is the target component i.putExtra("Value1","This is ActivityTwo"); i.putExtra("Value2","This Value two for ActivityTwo"); startactivity(i);
Implicit Intent:
An Implicit Intent permits you to declare the action you want to carry out. Further, the Android system will check which components are registered to handle that specific action based on intent data. Here target component is not defined in the intent.
For example,
Intent i = new Intent(ACTION_VIEW,Uri.parse("http://www.interview bit.com")); startActivity(i);
10. What is ANR in Android? What are the measures you can take to avoid ANR?
ANR(Application is Not Responding) is a dialog box that appears when the application is not responding. This ANR dialogue is displayed whenever the main thread within an application has been unresponsive for a long time under the following conditions:
When there is no response to an input event even after 5 seconds.
When a broadcast receiver has not completed its execution within 10 seconds.
Following measures can be taken to avoid ANR:
An application should perform lengthy database or networking operations in separate threads to avoid ANR.
For background task-intensive applications, you can lessen pressure from the UI thread by using the IntentService.
11. What are the troubleshooting techniques you can follow if an application is crashing frequently?
If an Android application is crashing frequently, you can follow the below-given techniques:
Compatibility Check:
It is not possible to test an application for all kinds of devices and operating systems. There might be a possibility that an application is not compatible with your OS.
Memory Management:
Some apps run perfectly on one mobile device but might crash on other devices. This is where processing power, memory management, and CPU speed are considered.
As there is a limited amount of memory space on mobile devices, you can free up memory space for the application to function properly.
If an application is frequently crashing, you can delete the application’s data, which will clear its cache memory and allow some free space on your device and might boost the app’s performance.
12. Explain different launch modes in Android.
Activity launch modes determine how Android handles the launching of a new instance of an activity when it's already present in the task stack. There are four main launch modes:
Standard: This is the default launch mode. Each time you start the activity, a new instance is created and added to the task stack. This can lead to multiple instances of the same activity in the stack.
SingleTop: In this mode, if a new instance of the activity is requested and the current instance is already at the top of the stack, the current instance will be reused (instead of creating a new one). If not at the top, a new instance will be created.
SingleTask: This mode ensures that only one instance of the activity exists in the entire task stack. If an instance already exists, the system will bring it to the foreground (reusing it) instead of creating a new one.
SingleInstance: Similar to SingleTask, but more isolated. An activity with this launch mode will create a new task, and only that activity will exist in that task. It's rarely used.
Choosing the right launch mode depends on the navigation flow you want to achieve. Standard is often suitable for most cases. SingleTop and SingleTask can be used when you want to control how instances are reused. SingleInstance is quite specialized and usually reserved for specific scenarios.
Remember, launch modes play a crucial role in managing the task and activity stack behavior in your Android app.
13. What are containers?
1. Definition:
Containers in Android refer to specialized components that can hold and organize other UI elements or views.
2. Layout Structure:
They act like boxes to arrange and manage how different elements, like buttons or text, are positioned and displayed on the screen.
3. Nested Organization:
Containers can be nested inside each other, allowing for a hierarchical structure to design complex and structured user interfaces.
4. Common Examples:
- LinearLayout: Arranges elements in a single line either horizontally or vertically.
- RelativeLayout: Positions elements relative to each other or the parent container.
- FrameLayout: Places elements on top of each other, useful for layering.
5. UI Flexibility:
Containers provide flexibility in designing diverse layouts, helping developers create visually appealing and responsive Android applications.
14. What is the role of Dalvik in Android development?
Dalvik serves as a virtual machine, and it is responsible for running every Android application. Because of Dalvik, a device will have the ability to execute multiple instances of virtual machines efficiently through better memory management.