"}},{"@type":"Question","name":"What are the lifecycle methods of Android activity and what is their purpose?","acceptedAnswer":{"@type":"Answer","text":"The lifecycle methods of Android activity are onCreate(), onStart(), onRestart(), onResume(), onPause(), onStop(), and onDestroy(). Their purpose is to help structure your code around how you want an activity to perform throughout its lifecycle on the device."}},{"@type":"Question","name":"What is an Application Not Responding (ANR) error, and how can you prevent them from occurring in your app?","acceptedAnswer":{"@type":"Answer","text":"An ANR dialog appears when your UI has been unresponsive for more than 5 seconds, usually because you've blocked the main thread. To avoid encountering ANR errors, you should move as much work off the main thread as possible."}},{"@type":"Question","name":"What’s the difference between onCreate() and onStart()?","acceptedAnswer":{"@type":"Answer","text":"The onCreate() method is called once during the Activity lifecycle, either when the application starts, or when the Activity has been destroyed and then recreated, for example during a configuration change. The onStart() method is called whenever the Activity becomes visible to the user, typically after onCreate() or onRestart()."}},{"@type":"Question","name":"What is the relationship between the lifecycle of an AsyncTask and an Activity? What problems can this result in? How can these problems be avoided?","acceptedAnswer":{"@type":"Answer","text":"
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). 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 Activty, which prevents the Activity from being garbage collected as long as the AsyncTask remains alive.
"}},{"@type":"Question","name":"What is difference between Serializable and Parcelable? Which is the best approach in Android?","acceptedAnswer":{"@type":"Answer","text":"Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations. Parcelable is an Android specific interface where you implement the serialization yourself. It was created to be far more efficient than Serializable, and to get around some problems with the default Java serialization scheme."}},{"@type":"Question","name":"What is the difference between Service and IntentService? How is each used?","acceptedAnswer":{"@type":"Answer","text":"Service is the base class for Android services that can be extended to create any service. A class that directly extends Service runs on the main thread so it will block the UI (if there is one) and should therefore either be used only for short tasks or should make use of other threads for longer tasks. IntentService is a subclass of Service that handles asynchronous requests (expressed as \"Intents\") on demand."}},{"@type":"Question","name":"How do you supply construction arguments into a Fragment?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"What is a Broadcast receiver?","acceptedAnswer":{"@type":"Answer","text":"A broadcast receiver is a component that responds to system-wide broadcast announcements. Many broadcasts originate from the system — for example, a broadcast announcing that the screen has turned off, the battery is low, or a picture was captured. Applications can also initiate broadcasts — for example, to let other applications know that some data has been downloaded to the device and is available for them to use. A broadcast receiver is implemented as a subclass of BroadcastReceiver and each broadcast is delivered as an Intent object."}},{"@type":"Question","name":"What are Views and View Groups and what's the difference between them?","acceptedAnswer":{"@type":"Answer","text":"A View in Android is the most basic element to build a UI element. A ViewGroup extends from the View class. A ViewGroup is the base class used to create Layouts, which are containers for different sets of Views (or also ViewGroups )."}},{"@type":"Question","name":"Explain AndroidManifest.xml file and why do you need this?","acceptedAnswer":{"@type":"Answer","text":"Every application must have an AndroidManifest.xml file in the root directory. It contains the information about your app and provides the same to the Android system. The information includes the package name, Android components such as Activity, Services, Broadcast Receivers, Content Providers, etc. Every Android system must have this information before running any app code."}},{"@type":"Question","name":"What are the different data storage options available on the Android platform?","acceptedAnswer":{"@type":"Answer","text":"The Android platform provides a wide range of data storage options. These options must be used based on the need such as whether the data is secure and should be used with permission only, or can be accessed publicly. Below is the list of data storage options on the Android platform:
SharedPreference: It stores data in XML files. It is the simplest way to store private data in the key-value pair.
SQLite: It stores structure data in the private database.
Internal Storage: It stores data in the device file system and other apps cannot read this data.
External Storage: Data is stored in the file system but it is accessible to all apps in the device.
"}},{"@type":"Question","name":"Imagine you need to create an Android app that supports multiple languages and cultures, what's the best approach for you to do this?","acceptedAnswer":{"@type":"Answer","text":"
First we need to make sure that we don't have any hardcoded strings inside our app. All strings should be put inside values => strings.xml file. If we want to create translation into Spanish language, we will create a new folder called values-es and create a new strings.xml file inside this folder. The newly created strings file will contain the same string Ids but the value will be translated into Spanish. values>strings.xml
<resources>
<string name=\"thank_you\">Thank You</string>
</resources>
values-es>strings.xml
<resources>
<string name=\"thank_you\">Gracias</string>
</resources>
"}},{"@type":"Question","name":"How to create application that support different screen sizes?","acceptedAnswer":{"@type":"Answer","text":"
Create different layout folder according to the screen sizes.
MyProject/
res/
layout/ # default (portrait)
main.xml
layout-land/ # landscape
main.xml
layout-large/ # large (portrait)
main.xml
layout-large-land/ # large landscape
main.xml
Create different bitmaps according to the screen densities.
MyProject/
res/
drawable-xhdpi/
awesomeimage.png
drawable-hdpi/
awesomeimage.png
drawable-mdpi/
awesomeimage.png
drawable-ldpi/
awesomeimage.png
"}},{"@type":"Question","name":"Can you mention some of the characteristics of the OO Programming languages?","acceptedAnswer":{"@type":"Answer","text":"The Object Oriented Programming (OO) is a modeling paradigm to build software that is more modular, easier to maintain and more flexible. They have the following features.
Encapsulation: OO provide objects the ability to encapsulate their internal behavior and attributes into classes. The attributes are hidden, and their state can only be modified through the usage of accesor functions. This increases modularity and maintenance of the code, since objects cannot interact between them in an undesired way.
Polymorphism: is the ability of the OO to present the same adaptable interface to different data types. A polymorphic function or type will be adaptable based on the data type that is requesting it.
Inheritance: this is the base of reusing code. Using inheritance we can create relationship between objects that extend from parent objects, and inherit their behavior and internal structure.
Abstraction: abstracting an object means to separate the idea of an object from the implementation details.
"}},{"@type":"Question","name":"What is the difference between an interface and an abstract class?","acceptedAnswer":{"@type":"Answer","text":"An interface is absolutely abstracted, and cannot be implemented. It defines the behavior that an object will need to perform, without providing any details about it. An abstract class cannot be instantiated, but it can partially or totally define behavior and internal structure for an object. Interfaces are always implemented, and objects always do extend from abstract classes."}},{"@type":"Question","name":"What does the keyword synchronized mean?","acceptedAnswer":{"@type":"Answer","text":"Synchronized methods enable a simple strategy for preventing thread interface and memory consistency errors. If an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods. When you have two threads that are reading and writing to the same resource, say a variable named foo, you need to ensure that these threads access the variable in an atomic way. Without the synchronized keyword, your thread 1 may not see the change thread 2 made to foo, or worse, it may only be half changed. This would not be what you logically expect."}},{"@type":"Question","name":"What is a memory leak?","acceptedAnswer":{"@type":"Answer","text":"Memory leaks happen when you hold on to an object long after its purpose has been served. Every object has got its own lifetime, after which it needs to say goodbye and leave the memory. But if some other object(s) is holding onto this object (directly or indirectly), then the garbage collector will not be able to collect it."}},{"@type":"Question","name":"Write some code example that will cause a java memory leak.","acceptedAnswer":{"@type":"Answer","text":"
BufferedReader br = new BufferedReader(new FileReader(inputFile));...
} catch (Exception e) {
e.printStackTrace();
}
Or a static final field holding a reference to an object:
class MemorableClass {
static final ArrayList list = new ArrayList(100);
}
"}},{"@type":"Question","name":"How would you launch an ActivityB from ActivityA and clear the back stack of activities when the new activity is called?","acceptedAnswer":{"@type":"Answer","text":"
Intent intent= new Intent(ActivityA.this, ActivityB.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finish();
"}},{"@type":"Question","name":"How does RecyclerView differ from ListView?","acceptedAnswer":{"@type":"Answer","text":"
A RecyclerView recycles and reuses cells when scrolling. This is a default behavior. It’s possible to implement the same in a ListView too but we need to implement a ViewHolder there
A RecyclerView decouples list from its container so we can put list items easily at run time in the different containers (LinearLayout, GridLayout) by setting LayoutManager
Animations of RecyclerView items are decoupled and delegated to ItemAnimator
"}},{"@type":"Question","name":"What kind of tests can you write to test your Android application?","acceptedAnswer":{"@type":"Answer","text":"Your app should include the three categories of tests: small, medium, and large:
Small tests are unit tests that you can run in isolation from production systems. They typically mock every major component and should run quickly on your machine.
Medium tests are integration tests that sit in between small tests and large tests. They integrate several components, and they run on emulators or real devices.
Large tests are integration and UI tests that run by completing a UI workflow. They ensure that key end-user tasks work as expected on emulators or real devices.
"}},{"@type":"Question","name":"What is Dependency Injection?","acceptedAnswer":{"@type":"Answer","text":"Dependency Injection is a design pattern to implement inversion of control, and to resolve dependencies. Dependency Injection (DI) eliminates boilerplate code (for example, by removing listener) and provides a much cleaner and more effective code. There are a few DI libraries used in Android development:
Dagger
ButterKnife
RoboGuice
"}},{"@type":"Question","name":"Are SQL Injection attacks valid in Android? How would you prevent them?","acceptedAnswer":{"@type":"Answer","text":"If you are using data and retrieving it from components or network components that at the end perform an SQL query, SQL injections are an issue. Besides using validation in input fields or libraries to avoid SQL injections, another possible solution is to use parameterized queries with ContentProviders, which virtually remove the risk of suffering an SQL Injection."}},{"@type":"Question","name":"What Security measures you should take to prevent reverseengineering on your APK?","acceptedAnswer":{"@type":"Answer","text":"There are several security measures we can take to make our code more difficult to reverse by an attacker, but the main answer is to use ProGuard. ProGuard is a Java obfuscator provided in most of the Android environments, which increases security in our code by performing different obfuscation techniques in our code."}}]]}]