Skip to content

Instantly share code, notes, and snippets.

@rahulkhatri19
Last active February 7, 2020 12:07
Show Gist options
  • Save rahulkhatri19/9e366eee908a6d2e514a4b7463147a02 to your computer and use it in GitHub Desktop.
Save rahulkhatri19/9e366eee908a6d2e514a4b7463147a02 to your computer and use it in GitHub Desktop.
Android Concept: interview preparation questions.
Dagger Dependency Injection
1. Setting Up Dagger:
app: build.gradle
apply plugin: 'kotlin-kapt'
ext {
dagger_version = '2.17'
auto_value_version = '1.6.2'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kapt "com.google.dagger:dagger-compiler:$dagger_version"
implementation "com.google.dagger:dagger:$dagger_version"
2. Essential Dagger:
Graph like code Structure -> Dagger has graph like code structure which is Direct Acyclic Graph (DAG) names it dagger.
Factory: way of creating dependency is a factory.
@Injector:
It will create object and there dependency.
class Client @Inject constructor(private val connection: Connection) {}
@Component:
It is a top level factory which can get object which are required.
3. It has different type:
a. Method Injection: property setter
// setting up setter for menthod injection.
@Inject constructor()
var client: Network.Client? = null
@Inject set
private var presenter = DaggerPresenterFactory.create().presenter()
b. Field Injection: properiy itself so it is lateinit.
// Create Abstract Factoty
@Component
interface PresenterFactory {
fun presenter(): Presenter
fun inject(act: MainActivity)
}
@Inject
lateinit var presenter: Presenter
DaggerPresenterFactory.create().inject(this)
c. Lazy Injection:
A Lazy<t> is a factory for object of type T. Used when code need lazy created singleton of type t.
class Presenter @Inject constructor( private val client: Lazy<Client>){
fun connect(show: (String?) -> Unit) {
val data = client.get().fetchData()
show(data)
}
}
Note : necessary to import dagger.Lazy // as is not imported by default.
d. Provider Injection: New connection to serever every new client request ie single instance of client object need to create new instance of connnection call to fetch data.
A Provider<T> is factory for object of type T. Used when code depends on multiple ojects of type t,
class Client @Inject constructor(private val connection: Provider<Connection>) {}
Article :
1. https://github.com/anitaa1990/Android-Cheat-sheet/blob/master/README.md
2. Interview Question Basic: https://www.softwaretestinghelp.com/android-interview-questions/
3. Interview Question 2 yr exp: https://blendinfotech.com/android-interview-questions-and-answers-for-2-years-experience
key skill : Learn Dagger, RxJava, Kotlin, MVVM, Architecture Components, Unit Testing.
1. Difference in activity and services
To answer this question you should have understanding of what activity and services do. Once you understand that it will be very easy to explain it.
For better understanding: https://stackoverflow.com/questions/5050005/what-is-the-difference-between-an-android-activity-and-service.
2. Difference in Activity context Vs Application context
Ans. https://blog.mindorks.com/understanding-context-in-android-application-330913e32514
3. How Android system Render/draw layout on screen
https://developer.android.com/guide/topics/ui/how-android-draws
Ans. When an Activity receives focus, it will be requested to draw its layout. The Android framework will handle the procedure for drawing, but the Activity must provide the root node of its layout hierarchy.
Drawing begins with the root node of the layout. It is requested to measure and draw the layout tree. Drawing is handled by walking the tree and rendering each View that intersects the invalid region. In turn, each ViewGroup is responsible for requesting each of its children to be drawn (with the draw() method) and each View is responsible for drawing itself. Because the tree is traversed pre-order, this means that parents will be drawn before (i.e., behind) their children, with siblings drawn in the order they appear in the tree.
4. Nested Recycler view and Nested Scroll view — advantage Vs disadvantage or issues
Sol.
Issue RV and SV : As we already know that recyclerView has a smooth scrolling by itself but when we need to put recyclerView within any scrollView it will not working.
Sol to issue: Use Nested Scroll view.
5. Given an Android app which is very slow ,For ex — UI is lagging and user has to wait a lot to see data on screen. Or May be list view scroll is slow. Where should we start looking into to find the reason.
Approach: To answer this question you must know how UI rendering happens in android. For ex you can have thousands of item to show in list, but on phones at a time limited set of data is shown. So suppose you open the screen and then you try to load data and do some parsing on those data, you have to be a little smart while doing all these. If you process all data at once it will be performance heavy and will lead to bad User experience. In such cases you should use pagination or Android Paging library. To understand pagination check my post : https://medium.com/@thegraduateguy/pagination-with-recyclerview-in-android-2506c4d09a5c
Further you should avoid any long running operation on UI thread.
6. Issues with Async task
https://blog.danlew.net/2014/06/21/the-hidden-pitfalls-of-asynctask/
AsyncTask and Rotation, AsyncTasks and the Lifecycle, Cancelling AsyncTasks, Limitations on Concurrent AsyncTasks.
The modern AsyncTask is limited to 128 concurrent tasks, with an additional queue of 10 tasks (if supporting Android 1.5, it’s a limit of ten tasks at a time, with a maximum queue of 10 tasks). That means that if you queue up more than 138 tasks before they can complete, your app will crash. Most often I see this problem when people use AsyncTasks to load Bitmaps from the net.
7. Can we change UI from asynchronous task using application context
Yes, Implementation would be AsyncTask or RxJava.
8. Design handler thread in java
To answer this one should know how handler thread works in Android. For more information about Handler Thread from Google docs and you can also refer my previous article at below link
https://medium.com/@thegraduateguy/understanding-handler-looper-and-handler-thread-5cd9bf444802
You can read about it more and then formulate your answer.
9. Activity Launch Modes
https://developer.android.com/guide/components/activities/tasks-and-back-stack
https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en
10. Activity/Fragment LifeCycle
Activity: https://developer.android.com/guide/components/activities/activity-lifecycle
Fragment: https://developer.android.com/guide/components/fragments
11. Permission handling in Android
Reference: https://developer.android.com/training/permissions/requesting
12. Difference in Service and Intent Service
https://www.linkedin.com/pulse/service-vs-intentservice-android-anwar-samir/
13. Options in Android to do long running task.
Ans: Handler thread, Async task, Services ,Traditional Java threads
Be careful while using services as by default it runs on main thread so to do any long running task one must create a separate thread within service,
14. Does activity instance gets garbage collected after rotation . If activity reference is in Asynctask , will it cause null pointer after rotation?
Yes activity can get garbage collected if it’s recreated after rotation and there is no reference of it. If this is used as a weak reference it can cause null pointer exception
15. Does two activity has same UI thread ?
NO. There is only one UI thread in Android System.
16. Difference in UI thread Vs main thread in Android
https://stackoverflow.com/questions/3261370/is-main-thread-the-same-as-ui-thread
17. What changes with respect to services came in Android P and Android O
Ref: https://developer.android.com/about/versions/oreo/background
W18. hat are content provider ?
Ref: https://developer.android.com/guide/topics/providers/content-provider-basics
19. What is inter app communication in Android and how to achieve it ?
Inter app communication is way in android by which two app can communicate and/or send data to each other. Two preferred way to do it Content Provider and AIDL
For ex whenever we want to see contact detail of any user we can query contact app using content provider.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment