Intro to Android

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

3074c66675d6c28231d50f91cb67a2ae.png

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.

5f74b1146359b2da39a899a5156f07a6.png

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

23c0e5afcf14c33b320504479143645f.png

  1. Create a new Activity class. Which either inherits Android.app.Activity or its subclasses.

  2. Override Activity.onCreate().

  3. Create a layout XML file in res/layout and use setContentView() to load this layout.

  4. Register the new activity in AndroidManifest.xml.

    If it is a main activity, you need to add a special section in the manifest file.

    5f50cb9f027a1bd432f0dc1ea3bb051c.png

Activities can be started by calling the function

1
startActivity(Intent intent)

To call Activity2 Inside an activity, do:

1
2
3
4
Intent intent =
new Intent(this, Activity2.class);

startActivity(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
2
3
Intent intent = new Intent(Intent.ACTION_SEND);
Intent.putExtra(Intent.EXTRA_EMAIL, recipientArray);
startActivity(intent);

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()

e3abb411a93727fb5764515c2ad064d3.png

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
2
startActivityForResult(Intent,
int), use finishActivity(int requestCode)

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

f7362876239d8ffc3874022bfed42ee0.png

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