Note that there are some explanatory texts on larger screens.

plurals
  1. POHow SurfaceHolder callbacks are related to Activity lifecycle?
    text
    copied!<p>I've been trying to implement an application that requires camera preview on a surface. As I see the things, both activity and surface lifecycles consist of the following states:</p> <ol> <li>When I first launch my Activity: <code>onResume()-&gt;onSurfaceCreated()-&gt;onSurfaceChanged()</code></li> <li>When I leave my Activity: <code>onPause()-&gt;onSurfaceDestroyed()</code></li> </ol> <p>In this scheme, I can do corresponding calls like open/release camera and start/stop preview in <code>onPause/onResume</code> and <code>onSurfaceCreated()/onSurfaceDestroyed()</code>.</p> <p>It works fine, unless I lock the screen. When I launch the app, then lock the screen and unlock it later I see:</p> <p><code>onPause()</code> - and nothing else after the screen is locked - then <code>onResume()</code> after unlock - and no surface callbacks after then. Actually, <code>onResume()</code> is called after the power button is pressed and the screen is on, but the lock screen is still active, so, it's before the activity becomes even visible.</p> <p>With this scheme, I get a black screen after unlock, and no surface callbacks are called.</p> <p>Here's a code fragment that doesn't involve actual work with the camera, but the <code>SurfaceHolder</code> callbacks. The issue above is reproduced even with this code on my phone (callbacks are called in a normal sequence when you press "Back" button, but are missing when you lock the screen):</p> <pre><code>class Preview extends SurfaceView implements SurfaceHolder.Callback { private static final String tag= "Preview"; public Preview(Context context) { super(context); Log.d(tag, "Preview()"); SurfaceHolder holder = getHolder(); holder.addCallback(this); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void surfaceCreated(SurfaceHolder holder) { Log.d(tag, "surfaceCreated"); } public void surfaceDestroyed(SurfaceHolder holder) { Log.d(tag, "surfaceDestroyed"); } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { Log.d(tag, "surfaceChanged"); } } </code></pre> <p>Any ideas on why the surface remains undestroyed after the Activity is paused? Also, how do you handle camera lifecycle in such cases?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload