Please enable JavaScript.
Coggle requires JavaScript to display documents.
Kotlin, Fragment is added - Coggle Diagram
Kotlin
-
function
-
-
-
crossline
Avoid non-local returns (not allow to put the return statement inside the lambda which is marked as crossline)
lamda
anonymous functions that can be treated as values or normal object: pass them as arguments to functions, return them...
suspend
could be started, paused & resume
-
-
-
-
-
-
-
object
object{}
-
-
-
-
If defined inside a class, can't skip the name while calling a method or accessing a variable
not have default name, must be named by us
lateinit
when to use
not want to initialize a variable at the time of the declaration
and initialize it at some later point in time
-
Variable is mutable, non-nullable & can be initialized later
-
lazy
-
used with the val keyword, hence read-only property
-
init{}
-
-
-
when to use
Need perform task during the initialization of object & don't have necessity for secondary constructor
companion object
-
can call method, variable without creating object of class
-
-
-
-
-
-
-
Platforms
Android
Components
Services
runs in background to perform long-running operations or to perform work for remote processes
Started services
-
Background Service
-
Less priority than Foreground Service, can be kiiled/restarted for other need of RAM or CPU
-
Deprecated
IntentService
- performe for a long time task
- always runs on the worker thread triggered from the main thread
- tasks will be performed on a queue basis
- Difficult to interact with the UI
replacement
- 1 more item...
Android >= O (Android 8.0, SDK 26)
-
Normal Service
- run on Main Thread, possible to block main thread
- for running not a very long task
- have to use stopService() or stopSelf() to stop service
- Easy to interact with the UI
Deprecated
JobIntentService
replacement
- 1 more item...
-
When to use what
For tasks that require user attention,
EX: play music in the background
-
For tasks that can run without user interaction,
EX:download data from a server
-
for tasks that require communication between a component and a service
EX: long-running task in the background and report the progress to the user
-
-
Content providers
manages a shared set of app data that can be stored in the file system, a SQLite database, on the web, or on any other persistent storage location that apps can access
-
-
Have access policies on data to ensure that sensitive data is not exposed to unauthorized applications
-
Intent
messaging object that is used to communicate between different components or between different apps
Explicit intent
Specify target component explicitly, by specifying the component's class name or package name. Typically used when starting specific activity or service within your own app
Implicit intent
Not specify target component explicitly, but instead describe general action to be performed, such as opening a web page or sending an email. The system then searches for the appropriate component to handle the Intent based on its action and data
Can carry data, such as text, images, or binary data
Used to trigger activities, services, and broadcasts, as well as to pass data between them
-
-
-
-
-
Views
-
-
ConstraintLayout
create dynamic, responsive layouts that can adapt to different screen sizes and orientations
-
SurfaceView
-
It provides a dedicated drawing surface, which is useful when a high frame rate and smooth animations are required, such as for games and video playback
when to use
Require high frame rate and smooth animations, such as for games and video playback
-
Jetpack
-
-
WorkManager
Better choice to replace Service, IntentService, JobScheduler to handle background tasks
Fragment
lifecycle
onAttach()
onCreate()
onCreateView()
onViewCreated()
onActivityCreated()
- 3 more items...
when is called
- 1 more item...
what to do within
- 1 more item...
-
what to do within
typically to inflate and configure a layout, return the root view
-
what to do within
Initializen on-UI elements, fragment's state and any other necessary resources, such as a database or network connection
-
-
-
-
FragmentManager
add()
when to use
add fragments on top of the previous fragment, such as when showing a dialog or a popup
replace()
when to use
clears all the previous Fragment then add it in FragmentContainer, such as when navigating to a new screen or view
-
-
Fragment Pager Adapter
-
FragmentPagerAdapter
-
when to use
have a small number of pages or simple fragments, and want smooth scrolling
-
-
retained Fragment
Calling setRetainInstance(true) allows to bypass destroy-and-recreate cycle of parent's activity, signaling the system to retain the current instance of the fragment when the activity is recreated
-
-
Activity
-
lifecycle
Activity starts
onCreate()
onStart()
onResume()
onPause()
- 5 more items...
-
Activity Resumed
- 1 more item...
called after the activity has been created but before it becomes visible on the screen, not yet visible to user
what to do within
- 1 more item...
when is called
- 1 more item...
Activity Started
corresponding to
- 1 more item...
-
-
Architecture
-
-
-
-
CLEAN
DI
Dagger-Hilt
Build on top of Dagger, specific for Android
comes with a built-in set of components & corresponding scope annotations that are automatically integrated into the various lifecycles of an Android app
-
Android Entry Points
- Activity
- Fragment
- View
- Service
- BroadcastReceiver1
-
-
-
-
-
-
Performance
why app lag
object allocation/deallocation occur more frequently, more frequently GC runs --> app unable update UI at the time GC runs --> drop frame
app doing to much/heavy tasks on main thread --> app unable update UI at the time task running -> drop frame
optimize
object allocation
allocate objects only when it is required, do not allocate a large number of unnecessary objects
-
Avoid Auto-Boxing as Integer, Boolean, ex: use int where ever possible instead of Integer
-
-
-
-
-
-
-
Use LRU cache for the bitmap to avoid redundant decoding of bitmap, it reduces GC calling again and again
-
Use Profile GPU Rendering gives quick visual representation of how much time it takes to render the frames of a UI window relative to the 16ms benchmark. Enable it from Settings -> Developer Options -> Monitoring Section -> Select Profile GPU Rendering
-
-
-
-
Provide access to resources, databases, and shared preferences, etc
when to use what
-
-
The Rule of Thumb
Always try to use the nearest context which is available:
- When in Activity, the nearest context is ActivityContext.
- When in Application, the nearest context is the ApplicationContext
Use the Context directly available from the enclosing component working within :
Application, Activity, Service, Broadcast Receiver
BaseContext
when need a generic context that can be used in situations where either an application context or an activity context is required
-
-
-
-
-
Publish
Từ 2019 trở đi : Mỗi năm yêu cầu về targetSdkVersion sẽ tăng lên. Trong vòng 1 năm sau mỗi lần Android release, app mới và app update sẽ cần chuyển target lên đúng API tương ứng.
-
-
-
-
-
Coroutines
-
-
-
-
-
-
-
Kotlin compiler write callback of suspend function under the hood. Coroutine calls callback continuation
-
-
-
-
Keyword
const
variable be inlined, will be no overhead to access that variable at runtime ---> better performance
Collections
List
toMap
associateBy(keySelector, valueTransform)
If some elements are having the same key returned by keySelector then, the last one will get added to the map
-
-
-
-
-