AmbientModeSupport

public final class AmbientModeSupport extends Fragment implements LifecycleOwner, ViewModelStoreOwner, HasDefaultViewModelProviderFactory, LifecycleOwner, SavedStateRegistryOwner, ActivityResultCaller

Object
   ↳ Fragment
     ↳ AmbientModeSupport

Use this as a headless Fragment to add ambient support to an Activity on Wearable devices.

The application that uses this should add the WAKE_LOCK permission to its manifest.

The following describes the general use of this class:

Create a subclass of one of the FragmentActivity classes and implement the AmbientCallbackProvider interface. Override the getAmbientCallback method to provide the callbacks required for reacting to the ambient events from the Android system. If a valid AmbientCallback is not provided (either no implementation of the AmbientCallbackProvider interface, or returning null from getAmbientCallback), then ambient mode will NOT be enabled.

The primary entry point for this code is the attach method. It should be called with an FragmentActivity as an argument and that FragmentActivity will then be able to receive ambient lifecycle events through an AmbientCallback. The FragmentActivity will also receive a AmbientController object from the attachment which can be used to query the current status of the ambient mode.

An example of how to implement the AmbientCallbackProvider interface, attach AmbientModeSupport to your FragmentActivity and use the AmbientController can be found below:

public class MyActivity extends FragmentActivity
    implements AmbientModeSupport.AmbientCallbackProvider {
    {@literal @}Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState)
        ...
        AmbientModeSupport.AmbientController controller = AmbientModeSupport.attach(this);
        boolean isAmbient = controller.isAmbient();
    }
    {@literal @}Override
    AmbientModeSupport.AmbientCallback getAmbientCallback() {
        return new AmbientModeSupport.AmbientCallback() {
            public void onEnterAmbient(Bundle ambientDetails) {...}
            public void onExitAmbient(Bundle ambientDetails) {...}
        }
    }
}

Summary

Nested types

AmbientModeSupport.AmbientCallback

Callback to receive ambient mode state changes.

AmbientModeSupport.AmbientCallbackProvider

Interface for any Activity that wishes to implement Ambient Mode.

AmbientModeSupport.AmbientController

A class for interacting with the ambient mode on a wearable device.

Constants

static final @NonNull String

Property in bundle passed to {@code AmbientCallback#onEnterAmbient(Bundle)} to indicate whether burn-in protection is required.

static final @NonNull String

Property in bundle passed to {@code AmbientCallback#onEnterAmbient(Bundle)} to indicate whether the device has low-bit ambient mode.

static final @NonNull String

Fragment tag used by default when adding AmbientModeSupport to add ambient support to a FragmentActivity.

Public constructors

Constructor

Public methods

static @NonNull AmbientModeSupport.AmbientController
<T extends FragmentActivity> attach(@NonNull T activity)

Attach ambient support to the given activity.

@Override @NonNull void
dump(
    @NonNull String prefix,
    @NonNull FileDescriptor fd,
    @NonNull PrintWriter writer,
    @NonNull Array<@NonNull String> args
)

Print the Fragments's state into the given stream.

@Override @CallSuper @NonNull void

Called when a fragment is first attached to its context.

@Override @CallSuper @NonNull void
onCreate(@NonNull Bundle savedInstanceState)

Called to do initial creation of a fragment.

@Override @CallSuper @NonNull void

Called when the fragment is no longer in use.

@Override @CallSuper @NonNull void

Called when the fragment is no longer attached to its activity.

@Override @CallSuper @NonNull void

Called when the Fragment is no longer resumed.

@Override @CallSuper @NonNull void

Called when the fragment is visible to the user and actively running.

@Override @CallSuper @NonNull void

Called when the Fragment is no longer started.

Inherited methods

From class ActivityResultCaller
abstract @NonNull ActivityResultLauncher<@NonNull I>

Register a request to start an activity for result, designated by the given contract.

abstract @NonNull ActivityResultLauncher<@NonNull I>

Register a request to start an activity for result, designated by the given contract.

From class ComponentCallbacks
From class Fragment
final @Override @NonNull boolean

Subclasses can not override equals().

final @Nullable FragmentActivity

Return the FragmentActivity this fragment is currently associated with.

@NonNull boolean

Returns whether the the exit transition and enter transition overlap or not.

@NonNull boolean

Returns whether the the return transition and reenter transition overlap or not.

final @Nullable Bundle

Return the arguments supplied when the fragment was instantiated, if any.

final @NonNull FragmentManager

Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

@Nullable Context

Return the Context this fragment is currently associated with.

@NonNull @Override ViewModelProvider.Factory

Returns the default that should be used when no custom Factory} is provided to the constructors.

@Nullable Object

Returns the Transition that will be used to move Views into the initial scene.

@Nullable Object

Returns the Transition that will be used to move Views out of the scene when the fragment is removed, hidden, or detached when not popping the back stack.

final @Nullable FragmentManager

This method is deprecated.

This has been removed in favor of getParentFragmentManager() which throws an IllegalStateException if the FragmentManager is null.

final @Nullable Object

Return the host object of this fragment.

final @NonNull int

Return the identifier this fragment is known by.

final @NonNull LayoutInflater

Returns the cached LayoutInflater used to inflate Views of this Fragment.

@Override @NonNull Lifecycle

Returns the Lifecycle of the provider.

@NonNull LoaderManager

This method is deprecated.

Use LoaderManager.getInstance(this).

final @Nullable Fragment

Returns the parent Fragment containing this Fragment.

final @NonNull FragmentManager

Return the FragmentManager for interacting with fragments associated with this fragment's activity.

@Nullable Object

Returns the Transition that will be used to move Views in to the scene when returning due to popping a back stack.

final @NonNull Resources

Return requireActivity().getResources().

final @NonNull boolean

This method is deprecated.

Instead of retaining the Fragment itself, use a non-retained Fragment and keep retained state in a ViewModel attached to that Fragment.

@Nullable Object

Returns the Transition that will be used to move Views out of the scene when the Fragment is preparing to be removed, hidden, or detached because of popping the back stack.

final @NonNull @Override SavedStateRegistry

Returns owned SavedStateRegistry

@Nullable Object

Returns the Transition that will be used for shared elements transferred into the content Scene.

@Nullable Object

Return the Transition that will be used for shared elements transferred back during a pop of the back stack.

final @NonNull String

Return a localized string from the application's package's default string table.

final @NonNull String
getString(
    @StringRes @NonNull int resId,
    @Nullable Array<@NonNull Object> formatArgs
)

Return a localized formatted string from the application's package's default string table, substituting the format arguments as defined in java.util.Formatter and format.

final @Nullable String

Get the tag name of the fragment, if specified.

final @Nullable Fragment

This method is deprecated.

Instead of using a target fragment to pass results, use setFragmentResult to deliver results to FragmentResultListener instances registered by other fragments via setFragmentResultListener.

final @NonNull int

This method is deprecated.

When using the target fragment replacement of setFragmentResultListener and setFragmentResult, consider using setArguments to pass a {@code requestKey} if you need to support dynamic request keys.

final @NonNull CharSequence
getText(@StringRes @NonNull int resId)

Return a localized, styled CharSequence from the application's package's default string table.

@NonNull boolean

This method is deprecated.

Use setMaxLifecycle instead.

@Nullable View

Get the root view for the fragment's layout (the one returned by onCreateView), if provided.

@MainThread @NonNull LifecycleOwner

Get a LifecycleOwner that represents the Fragment's View lifecycle.

@NonNull LiveData<@NonNull LifecycleOwner>

Retrieve a LiveData which allows you to observe the lifecycle of the Fragment's View.

@NonNull @Override