Package com.castlabs.android.player

## Class PlayerViewLifecycleDelegate

- java.lang.Object
- - com.castlabs.android.player.PlayerViewLifecycleDelegate

-     public class PlayerViewLifecycleDelegate
      extends Object

  The default lifecycle delegate.

  This class provides life cycle callbacks that can be used to manage the life cycle of the player controller in the context of an Activity that shows a `IPlayerView`.

  The default `PlayerView` exposes this through the `PlayerView.getLifecycleDelegate()` method. Use this to hook up activity lifecycle events.

  Note that the `PlayerViewLifecycleDelegate` does not handle the use-case when the Activity is completely destroyed and re-created with the subsequent `onCreate` call. To handle this the application may want to store the playback state upon `onSaveInstanceState` and use the state in `onCreate` call, for more details see the `PlayerView`

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class | Description
    --- | --- | ---
    `static interface` | `PlayerViewLifecycleDelegate.LifecycleListener` | Callback to receive delegated Lifecycle events.
    `static class` | `PlayerViewLifecycleDelegate.ResumeConfiguration` | Configuration object which will dictate the behaviour of the `PlayerController` whenever the enclosing

  - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `static PlayerViewLifecycleDelegate.ResumeConfiguration` | `DEFAULT_RESUME_CONFIGURATION` | Default `PlayerViewLifecycleDelegate.ResumeConfiguration`

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `PlayerViewLifecycleDelegate​(IPlayerView playerView)` | Create a new instance using the given player view

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `void` | `addLifecycleListener​(PlayerViewLifecycleDelegate.LifecycleListener lifecycleListener)` | Adds a lifecycle delegate listener
    `void` | `releasePlayer​(boolean background_playback)` | Call this method when the hosting activity stops.
    `void` | `removeLifecycleListener​(PlayerViewLifecycleDelegate.LifecycleListener lifecycleListener)` | Removes a `PlayerViewLifecycleDelegate.LifecycleListener`
    `void` | `resume()` | Call this method when the hosting activity resumes.
    `void` | `setResumeConfiguration​(PlayerViewLifecycleDelegate.ResumeConfiguration resumeConfiguration)` | Sets the `PlayerViewLifecycleDelegate.ResumeConfiguration`.
    `void` | `start​(Activity activity)` | Call this method when the hosting activity is started.

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_RESUME_CONFIGURATION

          public static final PlayerViewLifecycleDelegate.ResumeConfiguration DEFAULT_RESUME_CONFIGURATION

      Default `PlayerViewLifecycleDelegate.ResumeConfiguration`

  - ### Constructor Detail

    - #### PlayerViewLifecycleDelegate

          public PlayerViewLifecycleDelegate​(@NonNull
                                             IPlayerView playerView)

      Create a new instance using the given player view
      Parameters:
        `playerView` - The player view

  - ### Method Detail

    - #### start

          public void start​(@NonNull
                            Activity activity)

      Call this method when the hosting activity is started. This method sets up the devices screen to be kept on for the current Activity by setting the window flag `FLAG_KEEP_SCREEN_ON`. If you don't want to enforce this behaviour, consider clearing the window flags after a call to this method and then manually handle the flag. If you do clear the window settings, please ensure that you set the `FLAG_KEEP_SCREEN_ON` flag at least while playback is running using `getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);`. Usually one would overwrite the activity `Activity.onStart` method and delegate the event:

           @Override
            protected void onStart() {
                super.onStart();
                playerView.getLifecycleDelegate().start(this);
            }

      Parameters:
        `activity` - The Activity that hosts the player view

    - #### resume

          public void resume()

      Call this method when the hosting activity resumes. This method resumes playback from a saved state when `releasePlayer(boolean)` was called before and player state was saved.

      Note that the playback of a saved state is only resumed if the current player controller is not running in background (see `PlayerController.isBackgrounded()`).

      Usually one would overwrite the activities `Activity.onResume()` method and delegate the event:

           @Override
            protected void onResume() {
                super.onResume();
                playerView.getLifecycleDelegate().resume();
            }

      Please note that you should not call this method directly in the activities `onResume()` callback if you are using a `PlayerService`. In that case, use `onResume()` to (re)connect to the service and depending on the state of the service, call this method when the service is connected.

    - #### releasePlayer

          public void releasePlayer​(boolean background_playback)

      Call this method when the hosting activity stops. The method saves the player state so playback can be resumed later, when the user navigates back to the activity.

      If `background_playback` is `true`, the player controller will not be destroyed and should be handed off to the `PlayerService` to resume it from there later.

      If `background_playback` is `false`, the player controller will be destroyed using `PlayerController.destroy()`.

      Usually one would overwrite the activities `Activity.onStop()` method and delegate the event:

           @Override
            protected void onStop() {
                super.onStop();
                playerView.getLifecycleDelegate().releasePlayer(false);
            }

      Parameters:
        `background_playback` - True if the player will be send to background

    - #### addLifecycleListener

          public void addLifecycleListener​(@NonNull
                                           PlayerViewLifecycleDelegate.LifecycleListener lifecycleListener)

      Adds a lifecycle delegate listener
      Parameters:
        `lifecycleListener` - The listener

    - #### removeLifecycleListener

          public void removeLifecycleListener​(@NonNull
                                              PlayerViewLifecycleDelegate.LifecycleListener lifecycleListener)

      Removes a `PlayerViewLifecycleDelegate.LifecycleListener`
      Parameters:
        `lifecycleListener` - the Listener

    - #### setResumeConfiguration

          public void setResumeConfiguration​(@NonNull
                                             PlayerViewLifecycleDelegate.ResumeConfiguration resumeConfiguration)

      Sets the `PlayerViewLifecycleDelegate.ResumeConfiguration`.

      This configuration is used whenever `resume()` is called.

      Parameters:
        `resumeConfiguration` - the configuration to set