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