Android Technical Interview Questions Flashcards

1
Q

What is a ContentProvider & what is it used for?

A

A ContentProvider manages access to a structured set of data. It encapsulates the data & provides mechanisms for defining security. ContentProvider is the standard interface that connects data in one process with code in another process.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

List the methods in the Activity Lifecycle

A
  • onCreate()
  • onStart()
  • onResume()
  • onPause()
  • onStop()
    • onRestart()
  • onDestroy()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe a scenario where onPause() & onStop() would not be called before onDestroy() in the activity lifecycle.

A

if finish() is called from within the onCreate() method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why should you not rely on onDestroy() to manage resource destruction?

A

While onDestroy() is the last callback within the Activity Lifecycle, it may not always be called - instead, create & destroy resources in onStart(), onResume and onPause(), onStop() respectively.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe 3 common cases for using an Intent.

A

1) To start an Activity via startActivity()
2) To start a Service via startService()
3) To deliver a broadcast via sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an Intent? Can it be used to provide data to a ContentProvider? why or why not?

A

An intent is a common mechanism for starting a new Activity and transferring data between Activities - you cannot start a ContentProvider via an Intent. To access data in a ContentProvider, you must use the ContentResolver object via app Context to communicate with the provider as a client.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between an Activity and a Fragment?

A

Activity: A since, focused operation that a user can perform (e.g. dial a #, take a picture, send an email, etc) - however, there is nothing preventing the implementation of a more complex Activity

Fragment: A modular section of an Activity with its own lifecycle & input events which can be added/removed at will. A Fragment’s lifecycle is directly affected by its host Activity’s lifecycle.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between Serializable & Parcelable? Which is the best approach in Android?

A

Serializable - A standard Java interface that automatically serializes a marked class in certain situations

Parcelable - An Android-specific interface where you implement the serialization yourself

Parcelable was created to be far more efficient than Serializable & work around some issues with the default Java Serialization scheme.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is it possible to create an Activity in Android w/o a UI?

A

Yes, these activities are treated as abstract activities

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which method is called only once in a Fragment lifecycle?

A

onAttached()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a Broadcast Receiver?

A

A Broadcast Receiver communicates w/ the OS for things such as “check whether an internet connection is available”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is ANR & why does it happen?

A

ANR - Application Not Responding; means the UI thread is likely held up or the Activity is in onResume() while the user is interacting with the Activity.

This occurs due to starting heavy & long tasks on the main UI thread, rather than starting them in the background via AsyncTask

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between Service & IntentService?

A

Service - The base class for Android services that can be extended to create any service. Any class that extends Service runs on the main thread (& will block the UI if there is one) & therefore, should only be used for short tasks or use other threads for longer tasks

IntentService - Subclass of Service that handles async requests on demand. Writing an IntentService can be quite simple - just extend the IntentService class & override onHandleIntent(intent) where you can manage all incoming requests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are launch modes?

A

Launch modes are the way in which a new instance of an Activity is to be associated w/ the current task

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are the two mechanisms by which launch modes can be defined?

A

Launch modes may be defined using the AndroidManifest file or using Intent Flags that declare if & how the new Activity should be associated with the current task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the relationship between the lifecycle of an AsyncTask & an Activity?

A

An AsyncTask is not tied to the lifecycle of an Activity that contains it.

17
Q

What problems can arise from the relationship between an AsyncTask & an Activity?

A

If you start an AsyncTask inside an Activity & that Activity is destroyed, the AsyncTask will continue on until it completes, resulting in:

  • Updates to the former instance of the Activity, rather than the new one (leading to an Exception)
  • Potential memory leak since the AsyncTask maintains a reference to the Activity
18
Q

How can the problems from the relationship between an AsyncTask & an Activity be avoided?

A

The problems can be avoided by using a different mechanism for long-running background tasks (e.g. Service)

19
Q

Suppose you start a service in an Activity where the service accesses a remote server via internet connection. If the Activity is showing an animation that indicates some kind of progress, what issue might you encounter & how could you address it?

A

If a delayed response occurs, it could present that app as seeming to be frozen & locked up, or even worse - hang up the entire UI thread.

To address this, the network call should be done on a background thread via asynchronous mechanism.

20
Q

In an app you’re working on, you notice that a view’s value is not being restored after screen reorientation. What could be a likely cause of the problem that you should verify at a minimum, about that view?

A

Verify that the view has a valid ID (via android:id attributes)