Show Menu
Cheatography

Android Studio App Development Cheat Sheet Cheat Sheet (DRAFT) by

For my exam on Android Studio App Development.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Mobile App Develo­pment Lifecycle

Inception > Design(UX->UI) > Develo­pment > Stabil­ization > Distri­bution

IDEs for Mobile App Develo­pment

Android Studio (Java/­Kot­ilin), XCode (Objective C/Swift, can only run on MacOS), Microsoft XNA (C#, for Windows Phone apps), cocos2­d(m­ult­i-l­anguage and platform, for games), Unity (C#, cross-­pla­tform), React Native (JS, cross-­pla­tform), Flutter (Dart language), Apache Cordova (HTML5, CSS3 and JS, cross-­pla­tform), MiniPr­ogram (for making lightw­eight apps that don't require instal­lation and occupy little memory­/space)

Design Patterns

UI Design

Principles: User famili­arity, consis­tency, minimal surprise, recove­rab­ility, user guidance, user diversity

UI Design Process

Broadcasts

Broadcasts are messages sent whenever an event of interests occurs from the Android System or from apps. Apps can register to receive certain broadc­asts. They must be defined progra­mat­ically in the code in addition to being declared in the manifest with intent filters.
<in­ten­t-f­ilt­er>­<action androi­d:n­ame­="AC­TIO­N"/>­</i­nte­nt-­fil­ter>
Broadc­ast­Rec­eivers have an onRece­ive() function to check intent.ac­tion.

Server Connection

Must include the following permis­sions:
<us­es-­per­mission androi­d:n­ame­="an­dro­id.p­er­mis­sio­n.I­NTE­RNE­T" /> <us­es-­per­mission androi­d:n­ame­="an­dro­id.p­er­mis­sio­n.A­CCE­SS_­NET­WOR­K_S­TAT­E" />

WebView is a View that can be display web pages in your app.
 

Java vs Kotilin

Functions
public fun sum(a: Int, b: Int): Int { return a + b }
Variables
//val is immutable. var is mutable//
val a: Int = 1 val b: String = “tim”{nl} //Types Int, String are optional and can be excluded//
Nullable variable

var str S2? = "­hku­"
str = null
Null safety
var str: S2? = "­hku­" 
val I = S2?.length //if S2 is null, I is set o null
Arrays
val num = arrayOf(1, 2, 3, 4) //implicit type declar­ation 
val num = arrayO­f<I­nt>(1, 2, 3, 4) //explicit type declaration
for (i in 0..num.si­ze-1) { print(­num[i]) }
Classes
class Person(val name: String, val age: Int? = null) //Declares class and constr­uctor in one line!
val person1 = Person­(“Sam”, 20) //No new keyword
UI Elements
val edit_text1 = findVi­ewB­yId­<Vi­ew>­(R.i­d.e­di­tText1) as EditText
val button1 = findVi­ewB­yId­<Vi­ew>­(R.i­d.b­ut­ton1) as Button
val listener = View.O­nCl­ick­Lis­tener {edit_text1.setText("hello!")}
button1.setOnClickListener(listener)

Android Layout

Inflate the layout file in OnCrea­te():
public void onCrea­te(­Bundle savedI­nst­anc­eState) { super.o­nC­rea­te(­sav­edI­nst­anc­eSt­ate); setCon­ten­tVi­ew(­R.l­ayo­ut.a­ct­ivi­ty_­main);}

FrameL­ayout: Display a single item at a time. All elements are positioned based on top left of the screen, and multiple elements will overlap.
Linear­Layout: Organizes elements along a single line, either horizontal or vertical (defined in XML property
androi­d:o­rie­nta­tio­n="h­ori­zon­tal­"
)
Relati­veL­ayout:
androi­d:l­ayo­ut_­abo­ve/­bel­ow/­toL­eft­Of/­toR­ightOf

androi­d:l­ayo­ut_­ali­gnB­ase­lin­e/B­ott­om/­Lef­t/R­igh­t/Top

TableL­ayout:
<Ta­ble­Lay­out­><T­abl­eRo­w><­Element androi­d:l­ayo­ut_­col­umn­="1"/­></­Tab­leR­ow>...<­/T­abl­eLa­you­t>

Constr­ain­tLayout: Constrain a component to be in a position relative to another element. For example,
app:la­you­t_c­ons­tra­int­Lef­t_t­oRi­ght­Of=­"­@+i­d/e­lem­ent­"­/>
would constrain the left side of the target to the right side of the other element. Rember, it is app:layout, NOT androi­d:l­ayout.
Use sp for font size, as it scales with user font prefer­ences, and dp for others as it changes based on different screen density.

Concur­rency

A process is an instance of a program that is being executed or processed. They don't share resources. Switching between process is expensive. Threads are segments of processes and share memory. Thread­{...}.s­tart()
Main Thread is the UI thread which renders everything onscreen. Two rules of Android UI: Do not block the UI thread. Do not access the UI toolkit from outside the UI thread.
Runnable is a class that can be run inside a Thread with just 1 method: run(). Remember to use myUIEl­eme­nt.p­ost{ Runnable } to force the Runnable object to join a queue so as to not break the rule. Handlers can also be used to update the UI thread (
handle­r.post{ Runnable }
). Remember, Threads cannot update UI, only Handlers. Before running post() using Handler, call handle­r.r­emo­veC­all­bac­ks(­run­nable) to remove any pending posts of the runnable in the queue so as to avoid repeated task. handle­r.p­ost­Del­aye­d(r­unn­able, time) delays the runnable from starting until after the specified time.
 

Activity LifeCycle

Intents and Filters

Intents are messages sent between Activity, Service and Broad Receivers. Explicit intents are used within the applic­ation for tasks such as switching between activi­ties. They specify which component should be called. Kotilin:
intent = Intent­(this, FooAct­ivi­ty:­:cl­ass.java) startA­cti­vit­y(i­ntent);
Implicit intents only specify the action to be performed, and are sent to the Android system which chooses which component should be used. Kotilin:
intent = Intent­(In­ten­t.A­CTI­ON_­VIEW, Uri.pa­rse­(UR­LStr)); startA­cti­vit­y(i­ntent);

Activity needs Intent Filter to receive implicit intents.
<in­ten­t-f­ilt­er> <action androi­d:name = "­hku­cs.m­yi­nte­ntf­ilt­er" /> <ca­tegory androi­d:name = "­and­roi­d.i­nte­nt.c­at­ego­ry.D­EF­AUL­T" /> </i­nte­nt-­fil­ter>
Passing extra data: Sender:
myInte­nt.p­ut­Ext­ra(­"­ID", 6963)
Recpient:
val bundle: Bundle? = intent.extras
bundle?.let {bundl­e.apply { val inputS­tring: String? = getStr­ing­(“I­D") // id = 6963}}

Fragment

Must implement: onCreate() - called when creating the fragment, should initialize essential components you want to retain when the fragment is paused or stopped, then resumed. onCrea­teView - called when the fragment draws its user interface for the first time. Optional: onPause() - called when the user is leaving the fragment.
Kotilin implem­ent­ation
val manager: Fragme­ntM­anager = supportFragmentManager
val ft: Fragme­ntT­ran­saction = manager.beginTransaction()
if (fragment != null) ft.rep­lac­e(<ID of region in main layout­>, <class name of fragme­nt>(), <number tag to represent fragme­nt>); ft.com­mit­All­owi­ngS­tat­eLo­ss();

Service

StartS­erv­ice(): runs indefi­nitely even if caller app dies. Simple, single task. No return result. Cannot be called back or modified after it is sent out.
BindSe­rvice(): Can be bound to multiple compon­ents. Terminates if all callers die. Can be modified after being sent out.
A service can be both started and bound simula­ten­ously.
Activating service:
val intent = Intent­(this, HelloS­erv­ice­::c­las­s.j­ava); startS­erv­ice­(In­tent);

Return value of onStar­tCo­mmand():
START_­NOT­_ST­ICKY: Do not recreate after kill. Caller can restart unfinished jobs
START_­STICKY: Recreate, but do not redeliver last intent. Continuous work but stateless, e.g. media players.
START_­RED­ELI­VER­_IN­TENT: Recreate, and redeliver last intent. Actively performing a job, e.g. file download

Storage

Shared­Pre­fer­ences: Private primitive data in key-value pairs (persi­stent storage)
Bundles: Private primitive data in key-value pairs (temp storage for activi­ty-­fra­gment transfer)
Internal storage: Private data on device memory
External storage: Public data on internal or shared external storage, e.g., SD card
SQLite database: Structured data (table) in a private database