# Manage the camera lifecycle on Android

> Coordinate the host-owned Camera2 session with Nosmai processing so pause, lens switching, navigation, and cleanup keep one camera owner.

> For AI agents: the complete documentation index is at https://nosmai.com/llms/effects.txt

Product: Nosmai Effects
Group: guide
Source: https://nosmai.com/docs/effects/guides/camera/manage-camera-lifecycle/android/

## Prerequisites

- A working [Android quickstart](/docs/effects/quickstart/android/)
- One host-owned Camera2 helper connected to `NosmaiPreviewView`
- A serialized camera open, close, and lens-selection state machine

## Pause and resume the screen

The Android native SDK processes frames; the host application owns Camera2.
Close the repeating request and camera device before pausing the preview:

```java
@Override
protected void onPause() {
    camera2Source.close();
    previewView.onPause();
    super.onPause();
}
```

On resume, wait for the preview's OES surface callback and open the selected
camera against that current `SurfaceTexture`. Do not reuse a surface from a
destroyed view.

## Switch the camera

Stop the current capture session, close its device, select the other Camera2
ID, and reopen only after close completion. Keep the switch control disabled
during that transition. The host Camera2 helper—not `NosmaiSDK`—owns this lens
selection in the native Android integration.

## Release processing

Close Camera2 before stopping the processing pipeline:

```java
camera2Source.close();
NosmaiSDK.stopProcessing();
```

Do not call `NosmaiSDK.cleanup()` for every configuration change. Use global
cleanup only when the process no longer needs the initialized SDK state.

## Verify it worked

Switch lenses repeatedly, background and foreground the activity, then reopen
the screen ten times. Each transition must keep one Camera2 device open and the
camera indicator must turn off after leaving.

## Common errors

| Symptom | Cause | Fix |
| --- | --- | --- |
| `Camera device error: 2` | Another session still owns the selected device | Await Camera2 close before opening the Nosmai screen or other lens |
| Preview freezes after switching | The new session targets an old OES surface | Reopen against the current preview callback surface |
| Camera opens twice after resume | Activity and preview callbacks both start the source | Make one state machine the only Camera2 owner |

## Next steps

- [Record processed video on Android](/docs/effects/guides/output/record-video/android/)
- [Process external frames on Android](/docs/effects/guides/streaming/render-frames-off-screen/android/)

## References

- [Android API reference](/docs/effects/reference/api/android/)
- [Android platform guide](/docs/effects/platforms/android/)
