Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

51.

How to set app_name based on the built type and build flavor?

Answer»

For this, we can modify our BUILD Gradle file of the application. In Gradle file we will add CODE for ADDING build TYPE and build flavour like below:

buildTypes {    debug {        applicationIdSuffix '.debug'        versionNameSuffix '-DEBUG'        resValue "string", "app_name", "AppName debug"    }    release {        minifyEnabled true        shrinkResources true        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        signingConfig signingConfigs.release        zipAlignEnabled true        resValue "string", "app_name", "AppName"    } }
52.

How would you access data in a ContentProvider?

Answer»

In Android when we WANT to share data between two applications we are using Content Provider. It has a complete mechanism to share data between applications. It will also provide security while sharing data from one application to another application. For getting data from any Content Provider you need to create Content Resolver. For Creating Content Resolver, you need to call ACTIVITY’s getContentResolver method.

For Modifying data from Content Provider, you can invoke the basic method of Content Resolver like insert, delete, update and query. These operations are the same as the SQLite DB Operation.

Android we have two steps for RETRIEVING data from a Content Provider:

  • Write-read permission for READING data from Content Provider
  • Write a query to send a request to the content provider.
53.

You’re replacing one Fragment with another — how do you ensure that the user can return to the previous Fragment, by pressing the Back button?

Answer»

Android OS provides BACK stack function for Activity, it also provides back stack function for Fragment. If you add ONE Fragment into the back stack, when you PRESS the android device back menu, you can find the Fragment that is saved in back stack popup. Until all saved Fragments in back stack popup, then the activity will exit.

We have to create  “FragmentTransaction objects on the back stack and when the user presses the Back BUTTON, the “FragmentManager pops the most recent transaction off the back stack and performs the reverse action.

As mentioned in below code:

getSupportFragmentManager().beginTransaction()                           .add(detailFragment, "DETAIL")                           // Add this transaction to the back stack                           .addToBackStack(null)                           .commit();
54.

When should you use a Fragment, rather than an Activity?

Answer»

In Android, UI is representing as an Activity. Single Screen in an application represents as Activity while FRAGMENTS are reusable UI with business logic. Activity is a collection of Screen. In android application, Fragments are using in Navigation DRAWER in our entire screen. You can also use fragment to display Google Map on screen.

Following are important POINTS about a fragment:

  • The fragment life cycle is closely related to the LIFECYCLE of its host activity.
  • A fragment can implement a behaviour that has no user interface component.
  • A fragment has its own layout and its own behaviour with its own lifecycle callbacks.
  • You can add or remove fragments in an activity while the activity is running.
  • You can combine multiple fragments in a single activity to build a multi-pane UI.
  • A fragment can be used in multiple activities.
  • When the activity is PAUSED, all the fragments available in the activity will also be stopped.
55.

What is a fragment?

Answer»
  1. A Fragment represents a behavior or a portion of USER interface.
  2. A fragment must always be hosted in an activity and the fragment's lifecycle is directly affected by the host activity's life-cycle.
  3. When a fragment is ADDED as a PART of activity layout, it lives in a ViewGroup inside the activity's view hierarchy and the fragment defines its own view layout.
56.

What is a layout?

Answer»
  1. A layout defines the structure for a USER INTERFACE in an android app, such as in an activity.
  2. All ELEMENTS in the layout are built USING a hierarchy of View and ViewGroup OBJECTS.
  3. A View is visible and interact-able whereas a ViewGroup is an invisible container that defines the layout structure for View and other ViewGroup objects under it.
57.

What is the purpose of AndroidManifest.xml file?

Answer»

It is the FOUNDATION for any Android application. It will be in app module’s src/main/ directory for Android Studio projects. This file contains information of application package, INCLUDING components of the application such as activities, services, broadcast receivers, content providers etc. It performs some other tasks also: It is responsible to protect the application to ACCESS any protected parts by provision of permissions.

58.

What is the recommended way to perform discrete, ‘transactional’ background tasks in Android?

Answer»

The recommended way going forward is using WorkManager APIs, that makes it easy to specify deferrable, asynchronous tasks and when they should run under the CIRCUMSTANCES you choose, or RECURRING tasks that run at a specified INTERVAL. Internally WorkManager might use JOBSCHEDULER, Firebase-JobDispatcher, Executor, and AlarmManager for RUNNING the task.

59.

What is ART?

Answer»

It stands for Android RUN Time. ART is used for running Android applications. It uses AOT (AHEAD Of Time) compilation on the device. When an APK is getting installed on the Android device, the .dex files from the APK file will be converted to processor’s native instructions set/machine code (.art extension) and that will be stored in the app’s runtime cache directory on the device’s internal storage. This will make installation little bit longer and TAKE more space on the device storage but it makes apps run much faster, less BATTERY consumption, robots application debugging support and better user experience.

60.

How will you terminate an alive thread?

Answer»

There is no direct way to terminate a thread but using the two FOLLOWING ways it can be done. 

  1. Calling ‘interrupt()’ method on the thread object (e.g. threadObj), this will throw an InterruptedException inside the run method of thread. This exception will indicate that we should release the resources GRACEFULLY and then exiting the ‘run’ method of thread will terminate the thread.
  2. In case if the InterruptedException is swallowed by the code or ignored as well as If inside the thread’s ‘run’ method if any LOOPING is there, if YES then ADDING a method check in that loop that if this thread should exit now or not, is done by checking ‘Thread.currentThread().isInterrupted()’ that returns true and then exit the run method.