App Manifest
Declares several app components:
Activities:
-
The most fundamental component of the Android application model
-
Roughly corresponds to a screen-ful of user interaction.
-
An entry point for user interaction into the app
- Instead of a
mainmethod, there is a main activity declared in the manifest
- Instead of a
-
Subclasses of the
Activityclass- Or a specialized type like
AppCompatActivityorComponentActivity
- Or a specialized type like
-
Registration in app manifest:
<manifest ...> <application ...> ... <activity android:name=".NameOfActivityClass" android:exported="true"> <intent-filter> <!-- Register the activity as the one that executes on app launch --> <action android:name="android.intent.action.MAIN"/> <!-- Make the activity show up as an entry in the app launcher --> <category android:name="android.intent.category.LAUNCHER"/>` </intent-filter> </activity> </application> </manifest>
Fragments:
- Re-usable pieces of UI that are owned by an activity.
Services:
- Application component that performs long-running operations in the background
Content providers:
- Bridge between applications to share data
- Mostly built into the system; you do not usually create new ones yourself
Broadcast receivers:
- Components that responds to system-wide announcement
- e.g. battery low, screen off
- It is also possible to initiate broadcasts from within an application
Activity
Activity stack:
- Navigation represented as a stack of activities, with the current activity on top
- When going back, the top activity is popped off the stack and destroyed
- Beware of creating and pushed multiple instances of the same activity to the stack rather than popping to the existing instance
Lifecycle
┌──────────────► onCreate
│ │
│ │
│ ▼
│ onStart ◄──────────onRestart
│ │ ▲
│ │ │
│ ▼ │
│ onResume ◄───────┐ │
│ │ │ │
│ ▼ │ │
│ ┌────────────────┐ │ │
System │ Activity │ │ │
kills process │ running │ │ │
▲ └──────┬─────────┘ │ │
│ │ │ │
│ Different activity │ │
│ enters foreground │ │
│ │ │ │
│ │ │ │
│ ▼ user returns │
│ onPause──────────┘ │
│ | to activity │
│ Activity no │
│ longer visible │
│ │ │
│ low memory ▼ user navigates │
└─────────────── onStop───────────────────┘
│ to activity
│
Finished/destroyed
│
▼
onDestroy
onCreate:- Occurs on app launch, or if Android kills the process to free memory and the user returns to the app
onStart- An activity is stopped (
onStopped) if the activity is no longer visible. - If the user navigates back to the activity, it may be restarted (
onRestart)
- An activity is stopped (
onResume- A activity is paused (
onPaused) if another activity comes into the foreground - The activity may be partially obscured by another activity and interactions may be disabled
- When the user returns to the activity
- A activity is paused (
Primary states:
- Active:
- Activity is in the foreground, and user can interact with it
- Paused:
- Activity is partially obscured, user cannot interact with it (e.g. when menu/dialog open)
- Stopped:
- Completely hidden and not visible to the user
- Instance and variables retained, but no code is executed
- Examples of scenarios when an activity is stopped:
- User receives a phone call
- User performs an action that starts another activity in the application
- Phone rotation: activity (and the whole UI) is stopped and re-created
Lifetimes:
- Entire lifetime (
onCreate/onDestroy):- Load the UI
- Start and stop threads that should always be running
- Visible lifetime (
onStart/onStop)- Access or release resources that influence the UI
- Write to files if necessary
- Write information to files if necessary
- Foreground lifetime (
onResume/onPause)- Restore state and save state
- Access/release
- Broadcast receivers
- Sensors (e.g. GPS, camera)
- Start/stop video playback
- Start/stop animations
Programmatically stopping an activity:
- Generally shouldn’t be done: let Android manage it for you
- Call when you absolutely do not want the user to return to the instance of the activity
- Call the
finishorfinishActivitymethods
Architecture
Previously, the recommended architecture was a multiple activity architecture.
However, after the Navigation component Jetpack library was introduced in 2018, Google started to recommend a single activity-multiple fragment architecture.
This:
- Simplifies data sharing between different screens in an application using a
SharedViewModel - Simplifies the manifest file
Architectural principles:
- Separation of concerns: modularity, encapsulation
- Model-view design:
- Persistent model independent of views
- Allows multiple views of the same model (e.g. for different screen sizes)
- Each component has its own lifecycle
Views, View Groups and Layouts
- View: visible, interactive UI element
- ViewGroup: invisible container used to hierarchically and spatially organize views and view groups
- Usually called Layouts and implement defined layout structures (e.g.
LinearLayout,ConstraintLayout)
- Usually called Layouts and implement defined layout structures (e.g.
- Layouts can be declared as XML resources:
- Enables separation of presentation from behavior
- Allows different layouts to be created for different device sizes
XML
- Set activity view using
setContentView(R.layout.name_of_xml_layout_file), whereRis a file that has been automatically generated - Get a reference to an element using
findViewById(R.id.element_id)(where the XML element has theandroid:id="@=id/element_id"property)
Tasks:
- Use
Runnablecallbacks - Call
Handler(Looper.getMainLooper())to get a handle to the main thread - Use
handler.postorpostDelayedto execute the given runnable immediately or after some delay - Use
handler.removeCallbacksto remove the runnable (e.g. afteronStopis called)
Misc:
- Use the
lateinitmodifier on a property declaration to specify that you will always set the value before accessing it- Equivalent to ‘var varName!’ in Swift
- Adapter: data source for an array (or other data structure) that is used to populate views. Can be configured to take advantage of view recycling, click events etc.