RungHung Gau - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

RungHung Gau

Description:

onSaveInstanceState(), onRestoreInstanceState(), and Bundle. 3. Activity States. 4 ... Bundle can be seen as a handle to retrieve the stored state of the activity. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 18
Provided by: Mica3
Category:
Tags: runghung | bundle | gau | invoked

less

Transcript and Presenter's Notes

Title: RungHung Gau


1
Activity Lifecycle Management
  • Rung-Hung Gau
  • Department of Computer Science and Engineering
  • National Sun Yat-Sen University
  • Kaohsiung, Taiwan

2
Outline
  • Activity State
  • onCreate() abd onDestroy
  • onStart() and onStop()
  • onPause() and onResume()
  • onSaveInstanceState(), onRestoreInstanceState(),
    and Bundle

3
(No Transcript)
4
Activity States
  • Active the activity was started by the user, is
    running, and is in the foreground.
  • Paused the activity was started by the user, is
    running, and is visible, but a notification or
    something is overlaying part of the screen.
  • During this time, the user can see your activity
    but may not be able to interact with it.
  • For example, if a call comes in, the user will
    get the opportunity to take the call or ignore it.

5
Activity States
  • Stopped the activity was started by the user, is
    running, but it is hidden by other activities
    that have been launched or switched to.
  • In this case, your application will not be able
    to present anything meaningful to the user
    directly, only by way of a Notification .
  • Dead the activity was terminated, perhaps due to
    lack of available memory.

6
Activity States
  • Android will call into your activity (via
    callback methods related to lifecycle management,
    such as onCreate(), onPause(), and onDestroy())
    as the activity transits between the four states
    listed above.
  • Similar to Java ME lifecycle management methods
    (startApp(), pauseApp(), and destroyApp())
  • Some transitions may result in multiple calls to
    your activity, and sometimes Android will kill
    your application without calling it.

7
Activity States
  • This whole area is subject to change and you
    should pay attention to the official Android
    documentation.
  • Note that for all of these callback methods
    (lifecycle management methods), you should chain
    upward and invoke the superclass' edition of the
    method, or Android may raise an exception.

8
onCreate(Bundle icicle)
  • During the lifetime of an activity, the
    onCreate() method could be invoked multiple
    times.
  • Bundle can be seen as a handle to retrieve the
    stored state of the activity.
  • When the onSaveInstanceState() method is called
    (e.g., just before an activity is killed off),
    the state of the activity is stored via Bundle
  • When the activity is first started (e.g., since a
    system restart), onCreate(Bundle icicle) will be
    invoked with BundleNULL.

9
onCreate()
  • If the activity had been running, sometime later
    was killed off, and then re-created, onCreate()
    will be invoked with Bundle?NULL.
  • If the activity had been running and you have set
    up your activity to have different resources
    based on different device states (e.g., landscape
    versus portrait), your activity will be
    re-created and onCreate() will be called.

10
onDestroy()
  • onDestroy() may be called when the activity is
    shutting down, either because
  • the activity voluntarily called finish() to
    close itself
  • or because Android needs RAM and is closing the
    activity prematurely.
  • Note that onDestroy() may not get called if the
    need for RAM is urgent (e.g., incoming phone
    call) and that the activity will just get shut
    down regardless.
  • onDestroy() is mostly for releasing resources you
    obtained in onCreate() (if any).

11
onStart()
  • The onStart() method is called when an activity
    comes to the foreground (has the UI focus).
  • An activity can come to the foreground either
    because
  • it is first being launched,
  • or because it is being brought back to the
    foreground after having been hidden (e.g., by
    another activity, by an incoming phone call).

12
onStop() and onRestart()
  • onStop() is called when the activity is about to
    be stopped.
  • The onRestart() method is called in the case
    where the activity had been stopped and is now
    restarting.

13
onPause() and onResume()
  • The onResume() method is called just before your
    activity comes to the foregroun either
  • after being initially launched,
  • being restarted from a stopped state,
  • or after a pop-up dialog (e.g., incoming call) is
    cleared.
  • This is a place to refresh the UI based on things
    that may have occurred since the user last was
    looking at your activity.

14
onPause() and onResume()
  • Conversely, anything that steals your user away
    from your activity mostly, the activation of
    another activity will result in your onPause()
    being called.
  • Here, you should undo anything you did in
    onResume(), such as
  • stopping background threads,
  • releasing any exclusive-access resources you may
    have acquired (e.g., camera)
  • Once onPause() is called, Android reserves the
    right to kill off your activity's process at any
    point.

15
onSaveInstanceState()
  • Activities need to be able to save their
    application instance state and to do so quickly
    and cheaply.
  • Since activities could get killed off at any
    time, activities may need to save their state
    more frequently than one might expect.
  • Then, when the activity restarts (and calls
    onCreate()), the activity should get its former
    state back, so it can restore the activity to the
    way it appeared previously.

16
onSaveInstanceState()
  • Saving instance state is handled by
    onSaveInstanceState(). This supplies a Bundle,
    into which activities can pour whatever data they
    need (e.g., the number showing on the
    calculator's display).
  • This method implementation needs to be speedy.

17
onSaveInstanceState()
  • The application/activity instance state is
    provided to you in two places
  • onCreate()
  • onRestoreInstanceState()
  • It is your choice when you wish to re-apply the
    state data to your activity either callback is
    a reasonable option.
Write a Comment
User Comments (0)
About PowerShow.com