Skip to main content

Posts

Showing posts from 2021

how binding works in android

Why Use Data Binding Every developer wants clean and understandable code, but creating it is easier said than done. People rush, releases come one after another, clients want results and before you know it, your code is a mess. Knowing this, the Android team decided to make things easier by standardizing development. To that end, they launched the Jetpack libraries, which include the Data Binding Library. This library offers several advantages: Less code: By keeping code in activities and fragments small, it helps you write cleaner and more readable code. Fewer errors: The binding is checked at compile time. Faster apps: Since the binding isn’t done in onCreate, your app runs faster. Safer connection between views and code: Because it doesn’t bind at runtime, it’s safer to get the UI components than findViewById(). Safer connection between views and action: Data binding is safer than relying on onClick(), which ca...
Constraint layout Difference between constraint and relative layout Intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting. Rules remind you of RelativeLayout, for example setting the left to the left of some other view. app:layout_constraintBottom_toBottomOf="@+id/view1" Unlike RelativeLayout, ConstraintLayout offers bias value that is used to position a view in terms of 0% and 100% horizontal and vertical offset relative to the handles (marked with circle). These percentages (and fractions) offer seamless positioning of the view across different screen densities and sizes. app:layout_constraintHorizontal_bias="0.33" app:layout_constraintVertical_bias="0.53"
View holder pattern Why view holder pattern is important? As you fling through your ListView, there's only a handful of views being shown at any given time. This means that you don't have to instantiate a view for every item in your adapter; when a view scrolls off-screen, it can be reused, or recycled. View recycling and the ViewHolder pattern are not the same. The ViewHolder pattern is solely to reduce the number of view.findViewById(int) calls you make. The ViewHolder pattern only works when you take advantage of view recycling. In getView(int position, View convertView, ViewGroup parent), the convertView parameter is either null or it's a view that has been recycled: it will still have the data from a different list item bound to it. Without the ViewHolder pattern, you can still take advantage of view recycling (i.e. not blindly instantiating views):
Android vitals What are android vitals? The dashboard highlights crash rate, ANR rate , excessive wakeup , and stuck wake locks : these are the core vitals developers should give attention to. All other vitals, when applicable to your type of app or game, should be monitored to ensure they aren't having a negative effect. Exhibiting bad behavior in vitals will negatively affect the user experience in your app and is likely to result in bad ratings and poor discoverability on the Play Store. Excessive background Wi-Fi scans Excessive background network usage App startup time Slow rendering Frozen frames Permission denials
UI thread and backgound thread.. UI thread in Android When an app icon is clicked to launch it, system starts a new process and runs the main activity on a new thread if the app is not already started. All the subsequent user interactions with the app and components handle the interactions run on the the same thread. This thread is called main thread. If an app is already running on a thread and a component is requested, the component will run on the existing thread for the app. Background or worker thread can be created within the app to run long running tasks. Main thread is also called UI thread as all UI components run on the main thread. But in system apps, UI thread can be different from main thread if views run on different threads.
How will you track foreground and background events in android? Implementing a Foreground and Background Handler First, lets create our interface that will be implemented by a custom Application class. Something as simple as this: interface LifecycleDelegate { fun onAppBackgrounded() fun onAppForegrounded() } we could use onTrimMemory and the TRIM_MEMORY_UI_HIDDEN flag to detect background events. So lets do that now. Add this into the onTrimMemory method callback body if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { // lifecycleDelegate instance was passed in on the constructor lifecycleDelegate.onAppBackgrounded() } So now we have the background event covered lets handle the foreground event. To do this we are going to use the onActivityResumed. This method gets called every time any Activity in your app is resumed, so this could be called multiple times if you have multiple Activities. What we will do is use a flag...