Android Developer

  Home  Mobile Technologies  Android Developer


“Android Developer related Frequently Asked Questions by expert members with professional career as Android Developer. These list of interview questions and answers will help you strengthen your technical skills, prepare for the new job interview and quickly revise your concepts”



56 Android Developer Questions And Answers

22⟩ What is foreground Service?

A foreground service performs some operation that is noticeable to the user. For example, we can use a foreground service to play an audio track. A Notification must be displayed to the user.

 136 views

23⟩ Explain me what is an intent?

Intents are messages that can be used to pass information to the various components of android. For instance, launch an activity, open a webview etc. Two types of intents-

☛ Implicit: Implicit intent is when you call system default intent like send email, send SMS, dial number.

☛ Explicit: Explicit intent is when you call an application activity from another activity of the same application.

 133 views

24⟩ What are fragments?

Fragment is a UI entity attached to Activity. Fragments can be reused by attaching in different activities. Activity can have multiple fragments attached to it. Fragment must be attached to an activity and its lifecycle will depend on its host activity.

 134 views

25⟩ Do you know what is Application?

The Application class in Android is the base class within an Android app that contains all other components such as activities and services. The Application class, or any subclass of the Application class, is instantiated before any other class when the process for your application/package is created.

 140 views

27⟩ Explain me difference between AsyncTasks & Threads?

☛ Thread should be used to separate long running operations from main thread so that performance is improved. But it can’t be cancelled elegantly and it can’t handle configuration changes of Android. You can’t update UI from Thread.

☛ AsyncTask can be used to handle work items shorter than 5ms in duration. With AsyncTask, you can update UI unlike java Thread. But many long running tasks will choke the performance.

 157 views

28⟩ Explain me three common use cases for using an Intent?

Common use cases for using an Intent include:

☛ To start an activity: You can start a new instance of an Activity by passing an Intent to startActivity() method.

☛ To start a service: You can start a service to perform a one-time operation (such as download a file) by passing an Intent to startService().

☛ To deliver a broadcast: You can deliver a broadcast to other apps by passing an Intent to sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().

 155 views

29⟩ Do you know launch modes in Android?

☛ Standard: It creates a new instance of an activity in the task from which it was started. Multiple instances of the activity can be created and multiple instances can be added to the same or different tasks.

☛ Eg: Suppose there is an activity stack of A -> B -> C.

☛ Now if we launch B again with the launch mode as “standard”, the new stack will be A -> B -> C -> B.

☛ SingleTop: It is the same as the standard, except if there is a previous instance of the activity that exists in the top of the stack, then it will not create a new instance but rather send the intent to the existing instance of the activity.

☛ Eg: Suppose there is an activity stack of A -> B.

☛ Now if we launch C with the launch mode as “singleTop”, the new stack will be A -> B -> C as usual.

☛ Now if there is an activity stack of A -> B -> C.

☛ If we launch C again with the launch mode as “singleTop”, the new stack will still be A -> B -> C.

☛ SingleTask: A new task will always be created and a new instance will be pushed to the task as the root one. So if the activity is already in the task, the intent will be redirected to onNewIntent() else a new instance will be created. At a time only one instance of activity will exist.

☛ Eg: Suppose there is an activity stack of A -> B -> C -> D.

☛ Now if we launch D with the launch mode as “singleTask”, the new stack will be A -> B -> C -> D as usual.

☛ Now if there is an activity stack of A -> B -> C -> D.

☛ If we launch activity B again with the launch mode as “singleTask”, the new activity stack will be A -> B. Activities C and D will be destroyed.

☛ SingleInstance: Same as single task but the system does not launch any activities in the same task as this activity. If new activities are launched, they are done so in a separate task.

☛ Eg: Suppose there is an activity stack of A -> B -> C -> D. If we launch activity B again with the launch mode as “singleInstance”, the new activity stack will be:

☛ Task1 — A -> B -> C

☛ Task2 — D

 154 views

31⟩ What is fragment lifecycle?

☛ onAttach() : The fragment instance is associated with an activity instance.The fragment and the activity is not fully initialized. Typically you get in this method a reference to the activity which uses the fragment for further initialization work.

☛ onCreate() : The system calls this method when creating the fragment. You should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

☛ onCreateView() : The system calls this callback when it’s time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View component from this method that is the root of your fragment’s layout. You can return null if the fragment does not provide a UI.

☛ onActivityCreated() : The onActivityCreated() is called after the onCreateView() method when the host activity is created. Activity and fragment instance have been created as well as the view hierarchy of the activity. At this point, view can be accessed with the findViewById() method. example. In this method you can instantiate objects which require a Context object

☛ onStart() : The onStart() method is called once the fragment gets visible.

☛ onResume() : Fragment becomes active.

☛ onPause() : The system calls this method as the first indication that the user is leaving the fragment. This is usually where you should commit any changes that should be persisted beyond the current user session.

☛ onStop() : Fragment going to be stopped by calling onStop()

☛ onDestroyView() : Fragment view will destroy after call this method

☛ onDestroy() :called to do final clean up of the fragment’s state but Not guaranteed to be called by the Android platform.

 151 views

32⟩ Tell us what is the difference between a fragment and an activity? Explain the relationship between the two?

An activity is typically a single, focused operation that a user can perform (such as dial a number, take a picture, send an email, view a map, etc.). Yet at the same time, there is nothing that precludes a developer from creating an activity that is arbitrarily complex.

Activity implementations can optionally make use of the Fragment class for purposes such as producing more modular code, building more sophisticated user interfaces for larger screens, helping scale applications between small and large screens, and so on. Multiple fragments can be combined within a single activity and, conversely, the same fragment can often be reused across multiple activities. This structure is largely intended to foster code reuse and facilitate economies of scale.

A fragment is essentially a modular section of an activity, with its own lifecycle and input events, and which can be added or removed at will. It is important to remember, though, that a fragment’s lifecycle is directly affected by its host activity’s lifecycle; i.e., when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all of its fragments.

 173 views

33⟩ Tell us how do you supply construction arguments into a Fragment?

Construction arguments for a Fragment are passed via Bundle using the Fragment#setArgument(Bundle) method. The passed-in Bundle can then be retrieved through the Fragment#getArguments() method in the appropriate Fragment lifecycle method.

It is a common mistake to pass in data through a custom constructor. Non-default constructors on a Fragment are not advisable because the Fragment may be destroyed and recreated due to a configuration change (e.g. orientation change). Using #setArguments()/getArguments() ensures that when the Fragment needs to be recreated, the Bundle will be appropriately serialized/deserialized so that construction data is restored.

 133 views

34⟩ Please explain the difference between FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_CLEAR_TOP?

☛ FLAG_ACTIVITY_CLEAR_TASK is used to clear all the activities from the task including any existing instances of the class invoked. The Activity launched by intent becomes the new root of the otherwise empty task list. This flag has to be used in conjunction with FLAG_ ACTIVITY_NEW_TASK.

☛ FLAG_ACTIVITY_CLEAR_TOP on the other hand, if set and if an old instance of this Activity exists in the task list then barring that all the other activities are removed and that old activity becomes the root of the task list. Else if there’s no instance of that activity then a new instance of it is made the root of the task list. Using FLAG_ACTIVITY_NEW_TASK in conjunction is a good practice, though not necessary.

 167 views

35⟩ What is lifecycle of an Activity?

☛ OnCreate(): This is when the view is first created. This is normally where we create views, get data from bundles etc.

☛ OnStart(): Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

☛ OnResume(): Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.

☛ OnPause(): Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed.

☛ OnStop(): Called when you are no longer visible to the user.

☛ OnDestroy(): Called when the activity is finishing

☛ OnRestart(): Called after your activity has been stopped, prior to it being started again

 145 views

37⟩ Please explain what is an Intent? Can it be used to provide data to a ContentProvider? Why or why not?

The Intent object is a common mechanism for starting new activity and transferring data from one activity to another. However, you cannot start a ContentProvider using an Intent.

When you want to access data in a ContentProvider, you must instead use the ContentResolver object in your application’s Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.

 187 views

38⟩ Please explain how to prevent the data from reloading and resetting when the screen is rotated?

☛ The most basic approach would be to use a combination of ViewModels and onSaveInstanceState() . So how we do we that?

☛ Basics of ViewModel: A ViewModel is LifeCycle-Aware. In other words, a ViewModel will not be destroyed if its owner is destroyed for a configuration change (e.g. rotation). The new instance of the owner will just re-connected to the existing ViewModel. So if you rotate an Activity three times, you have just created three different Activity instances, but you only have one ViewModel.

☛ So the common practice is to store data in the ViewModel class (since it persists data during configuration changes) and use OnSaveInstanceState to store small amounts of UI data.

☛ For instance, let’s say we have a search screen and the user has entered a query in the Edittext. This results in a list of items being displayed in the RecyclerView. Now if the screen is rotated, the ideal way to prevent resetting of data would be to store the list of search items in the ViewModel and the query text user has entered in the OnSaveInstanceState method of the activity.

 188 views

39⟩ Tell us what is the relationship between the life cycle of an AsyncTask and an Activity? What problems can this result in? How can these problems be avoided?

An AsyncTask is not tied to the life cycle of the Activity that contains it. So, for example, if you start an AsyncTask inside an Activity and the user rotates the device, the Activity will be destroyed (and a new Activity instance will be created) but the AsyncTask will not die but instead goes on living until it completes.

Then, when the AsyncTask does complete, rather than updating the UI of the new Activity, it updates the former instance of the Activity (i.e., the one in which it was created but that is not displayed anymore!). This can lead to an Exception (of the type java.lang.IllegalArgumentException: View not attached to window manager if you use, for instance, findViewById to retrieve a view inside the Activity).

There’s also the potential for this to result in a memory leak since the AsyncTask maintains a reference to the Activity, which prevents the Activity from being garbage collected as long as the AsyncTask remains alive.

For these reasons, using AsyncTasks for long-running background tasks is generally a bad idea . Rather, for long-running background tasks, a different mechanism (such as a service) should be employed.

 151 views