Skip to content

Instantly share code, notes, and snippets.

View rohitjakhar's full-sized avatar
💭
Android Developer

Rohit Jakhar rohitjakhar

💭
Android Developer
View GitHub Profile
private val onDownloadCompleteReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent == null) {
log("onDownloadComplete intent is: $intent")
return
}
if (intent.action == DownloadManager.ACTION_DOWNLOAD_COMPLETE) {
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
val downloadedFileURI = mDownloadManager?.getUriForDownloadedFile(id)
viewModel.enqueuedDownloadingTasksIds.let { enqueuedTasks ->
@rohitjakhar
rohitjakhar / ExtensionFun.kt
Last active October 4, 2021 14:14
Firebase Realtime Genric Callback
@ExperimentalCoroutinesApi
inline fun <reified T> DatabaseReference.observeValue(): Flow<NetworkResponse<List<T>>> =
callbackFlow {
val listener = object : ValueEventListener {
override fun onCancelled(error: DatabaseError) {
trySend(NetworkResponse.Failure(message = error.message))
}
override fun onDataChange(snapshot: DataSnapshot) {
val hostList = arrayListOf<T>()

Android Interview Questions

Q1: Explain activity lifecycle ☆☆

Answer: As a user navigates through, out of, and back to your app, the Activity instances in your app transition through different states in their lifecycle.

To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of six callbacks: onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). The system invokes each of these callbacks as an activity enters a new state.

Interview Questions

Kotlin

Q1: What is a primary constructor in Kotlin? ☆☆

Answer: The primary constructor is part of the class header. Unlike Java, you don't need to declare a constructor in the body of the class. Here's an example:

package com.rohitjakhar.sagarkhurana.viewmodel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.cachedIn
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import com.rohitjakhar.sagarkhurana.data.RemoteDataStore
package com.rohitjakhar.sagarkhurana.ui.product
import android.content.Intent
import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DividerItemDecoration
val fetechAllData = Pager(PagingConfig(20)) {
ProductPagingSource(db)
}.flow.cachedIn(viewModelScope)
package com.rohitjakhar.sagarkhurana.adapter
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
package com.rohitjakhar.sagarkhurana.ui.addorder
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.navArgs
@rohitjakhar
rohitjakhar / HomeFragment.kt
Created March 28, 2021 22:42
Home Home Home
package com.rohitjakhar.blogx.ui.home
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager