Skip to content
  • Android
  • iOS
  • Web

Keep Awake

Keep a display awake while a screen or activity needs it.

Examples

Quick start

The hook releases its request when the activity stops or the component unmounts.

nayan-ui/keep-awake
import { useKeepAwake } from 'nayan-ui/keep-awake';
import { NText } from 'nayan-ui/components';

export function TimerScreen({ running }: { running: boolean }) {
  useKeepAwake(running);
  return <NText>{running ? 'Timer running' : 'Timer paused'}</NText>;
}

Keep the screen awake during an async task

nayan-ui/keep-awake
import { keepAwake } from 'nayan-ui/keep-awake';

export async function runWithScreenAwake<T>(run: () => Promise<T>) {
  if (!keepAwake.isAvailable()) return run();
  const tag = 'foreground-export';
  await keepAwake.activate(tag);
  try {
    return await run();
  } finally {
    await keepAwake.deactivate(tag);
  }
}

Tags