2. Outline
î What is activity
î Starting activity
î Shutting down activity
î Activity lifecycle
î Save state
î Back stack
î Tips
2
3. What is activity
î An application component
î A screen with which user can interact
î A window to draw its UI
3
4. Creating an activity
î Declare in manifest.xml
î Implement xml layout (optional)
î Extends from Activity
4
5. Starting an activity
î By name
Intent intent = new Intent(this, SignInActivity.class);
startActivity(intent);
î By action
Intent intent = new Intent(âcom.multi.action_showâ);
startActivity(intent);
5
6. Starting an activity (cont)
î Start for result
Intent intent = new Intent((Intent.ACTION_PICK);
intent.setType(âvideo/*â);
startActivityForResult(intent, requestCode);
protected void onActivityResult(int requestCode, int
resultCode, Intent data) {
}
6
7. Shutting down an activity
î Explicitly
î Finish();
î Implicitly
î Let the system do it (when user press BACK HW)
î Force Close
7
8. Activity lifecycle
î onCreate(Bundle savedInstanceState)
î initialize the essential components
î setContentView();
î onResume()
î just before the activity starts interacting with the
user (foreground)
î OnPause()
î Another activity is in the foreground + focus, but this
one is still visible => completely alive
î partially transparent or doesn't cover the entire 8
screen
9. Activity lifecycle (cont)
î OnStop()
î completely obscured by another activity
(=>background)
î is not attached to the window manager
9
14. Homework
î Given activities started in order: A->B-C->D
Draw the back stack to illustrate 2 cases:
î 1. Start new activity D
î 2. Start new activity B
with modes: standard, singleTop, singleTask,
singleInstance and Flag:
FLAG_ACTIVITY_NEW_TASK,
FLAG_ACTIVITY_SINGLE_TOP,
FLAG_ACTIVITY_CLEAR_TOP
Requirement:
î Draw back stack state when start new activity and
14
then press back HW until the program is closed