Application Components
Services
A service is an component that runs at the background
E.g. music play at the background
Service is not a thread
Broadcast Receiver
Receives broadcast from Android system and other apps and responses with pre-coded actions.
Apps are required to regist to get the broadcast
Example: When the battery level changes, the system broadcast a message.
Activity
Offen refer to one interface on your phone.
Primary class for interacting with user.
For example, Wechat:
When you click on the app launcher, its corresponding greeting page
will be shown to you.Android system invokes the main activity of the Wechat.
Apps that request for online payment (such as railway ticket
payment) can directly reach the payment page.Another app invokes the payment activity of Wechat.
Apps that request for “share on moments” can directly invoke the
moments sharing page of Wechat.Another app invokes the “share on moments” activity of Wechat
Back Stack
When a new activty is launched, the previous activity will be paused and sent to the top of the back stack
Life circle
Runing: The activity is on the top of the screen and gained focus. Running will not be killed.
Paused: The activity is on the top of the screen and gained focus.
Stopped: The activity is completely covered by another running activity.
Destroyed
When running short of memory, a stopped activity is more likely to get killed than a paused/running activity.
Good implementation of callback functions can make your app more robust and performant.
Possible issues with a bad implementation:
Crashing if the user receives a phone call or switches to another app while using your app.
Consuming valuable system resources when the user is not actively using it.
Losing the user’s progress if they leave your app and return to it at a later time.
Crashing or losing the user’s progress when the screen rotates between landscape and portrait orientation.
CallBack Funtions: Typical Uses
onCreate(): Initial setup, load persistent state.
onRestart(): read cached state
onStart(): reset application
onResume(): start foreground-only behaviors
onPause(): shutdown foreground-only behaviors
For example: temporarily stop UI animations.
onStop(): cache state
onDestroy(): save persistent state
The above points are very general. Carefully design your app and keep the life cycle graph in mind.
Create Activities
Create a new Activity class. Which either inherits Android.app.Activity or its subclasses.
Override Activity.onCreate().
Create a layout XML file in res/layout and use setContentView() to load this layout.
Register the new activity in AndroidManifest.xml.
If it is a main activity, you need to add a special
section in the manifest file.
Activities can be started by calling the function
1 | startActivity(Intent intent) |
To call Activity2 Inside an activity, do:
1 | Intent intent = |
The name of the target activity is not always explicitly specified. For instance, to let Android system choose an suitable activity for sending email (in Lecture 9):
1 | Intent intent = new Intent(Intent.ACTION_SEND); |
Obtain vals from another activity
Sometimes we wish to obtain results from another activity. We need to start the activity using
1 | startActivityForResult() |
You must also implement the function below to get the return result
1 | onActivityResult() |
Closing Activities
Android will automatically manage the life cycles of your activities.
You can destroy the current activity manually by calling finish()
.
To finish an activity that you previously invoked with
1 | startActivityForResult(Intent, |
Can be handy when you want to make sure that the user won’t return to this activity in the future.
Passing Data between Activities
Bundle and Intent is actually the same thing.
Console output
Your console output (System.out) can be seen from the “run” window in Android Studio.
You should normally use Log class instead, though:
Android Logcat | Log.v(), Log.d(), Log.i(), Log.w(), Log.e() - EyeHunts