Instantly share code, notes, and snippets.

View notes.md

In the broadest sense,

  • A future or promise can be thought of as a value that will eventually become available.
  • Future is used for sychronizing program execution.
  • Future acts as a proxy for an result which will be eventually available.
  • The construct ( future X ) immediately returns a future for the value of the expression X and concurrently begins evaluating X. When the evaluation of X yields a value, that value replaces the future.
  • A future is a placeholder object for a result that does not yet exist. A promise is a writable, single-assignment container, which completes a future. Promises can complete the future with a result to indicate success, or with an exception to indicate failure.
View file.md

Arrays are covariant but generics aren'. The following is the explanation behind using wildcard <? extends T> in generics.

At heart, these terms describe how the subtype relation is affected by type transformations. That is, if A and B are types, f is a type transformation, and ≤ the subtype relation (i.e. A ≤ B means that A is a subtype of B), we have

f is covariant if A ≤ B implies that f(A) ≤ f(B) f is contravariant if A ≤ B implies that f(B) ≤ f(A) f is invariant if neither of the above holds

Ref

View gradle_basics.md

Its a build system, used to automate and manage build process, while allowing you to define flexible custom build configurations.

Google saw one of the most advanced build systems on the market and realized that you could write scripts of your own with little to no learning curve, and without learning Groovy or any other new language. So they wrote the Android plugin for Gradle.

Gradle and android plugin help you configure the following:

Build Types

The android studio defines 2 build types, debug and release. you can define your own as well. You can configure each of them, For e.g. the debug build type enables debug options and signs the APK with the debug key, while the release build type may shrink, obfuscate, and sign your APK with a release key for distribution.

Product Flavors

View AvoidSetter.md

Some pointers

  • Having setters violates open/close principle, prevents information hiding (breaks encapsulation)
  • Discussing getter/setters vs public fields often obscures bigger problems with objects manipulating each others' internal state in an intimate manner and hence being too closely coupled. The idea is to make methods do what your business logic wants it to do, rather than have setters which change things at a field level.
  • One way to avoid writing setters is a task based approach to the model. Think of every task that is performed in an application and add a method that changes all the affected fields at once, to perform this task.

Ref:

View speed_up.md

Gradle tweaks

Open or create a file called gradle.properties in .gradle directory. Inside the file, add following

  • org.gradle.parallel=true: Allow you to build multiple modules in the same project at the same time
  • org.gradle.daemon=true will turn on daemon so that every time we build the application, it doesn’t need to rerun the entire Gradle application every time.

Memory Allocation tweaks

By default, Android Studio detects your installed RAM and configure the memory allocation automatically. However, the default value usually is too small to handle Android Studio. We can override it by opening it inside the Android Studio. Click Help > Edit Custom VM Options.

View menu.md

Menus are common user interface component in Android. It provides consistent user experience at the cost of flexibility. You should use the Menu APIs to present user actions and other options in your activities.

There are 3 types of Menu:

Options menu

The options menu is the primary collection of menu items for an activity. It's where you should place actions that have a global impact on the app, such as "Search," "Compose email," and "Settings."

Context menu and contextual action mode

A context menu is a floating menu, like dialog, that appears when the user performs a long-click on an element. It provides actions that affect the selected content or context frame.

View viewpager.md

A ViewPager is a ViewGroup that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.

Create a custom Pager Adapter

// override 4 methods as shown below
class MyPagerAdapter: PagerAdapter() {

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
View LaunchMode.md

Since each Activity is made to work in different purpose. Some is designed to work separately with each Intent sent for example an Activity for email composing in email client. While some is designed to work as a singleton for example an email's inbox Activity. That's why it does matter to specify whether Activity is needed to be created a new one or to use the existed one. launchMode is designed for this specifically.

launchMode can be assigned directly as an attribute tag

<activity
    android:name=".SingleTaskActivity"
    android:label="singleTask launchMode"
    android:launchMode="singleTask">
View markdown.md
  1. The following line is bold
    Hi, I am Bold (**Hi, I am Bold**)

  2. The following line is given emphasis
    I am cooool. (*I am cooool.*)

  3. To write something on the new line, give two spaces and hit enter
    Yay! we are on the next line

  4. Now comes the Headline