I have my Activity class here:
public class CameraActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// startCamera();
}
public void startCamera() {
setContentView(R.layout.camera_view);
mCamera = getCameraInstance();// Open Camera
mPreview = new CameraPreview(this,mCamera);// Goto Another Class
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.setLayoutParams(new FrameLayout.LayoutParams(400,400));
//Declare Frame in which camera will be opened
preview.addView(mPreview); // show this class into frame
}
protected void onResume() {
super.onResume();
Log.d("Print","resume()");
startCamera();
}
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
Toast.makeText(null,"No camera on this device",Toast.LENGTH_LONG).show();
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
/*
protected void onPause() {
if (mCamera!=null) {
mCamera.release(); // release the camera for other applications
mCamera = null;
}
super.onPause();
}
*/
}
And Camera Preview class here:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
@SuppressWarnings("deprecation")
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d("Print","Error setting camera preview: "+e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d("Print","Destroyed()");
mCamera.stopPreview();
mCamera.release();
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface()==null) {
return;
}
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d("Print","Error starting camera preview: "+e.getMessage());
}
}
}
However, when I am testing the class, onResume() seems to be called initially and then again after 1 or 2 seconds. So the Camera has to refresh once again. If I don't have onResume() at all, the camera preview is stable but then crashes if I switch to the app again from homescreen or some other app. I've found onPause() doesnt effect either of those. Is my code correct? What should I add/delete to make it not refresh again and still not crash after app switching?
This is the LogCat Output from 1st onResume() to another:
12-19 22:58:27.604: D/Print(28831): resume()
12-19 22:58:27.624: D/dalvikvm(29223): GC_CONCURRENT freed 195K, 8% free 3591K/3864K, paused 2ms+1ms, total 9ms
12-19 22:58:27.634: D/dalvikvm(29223): GC_FOR_ALLOC freed 245K, 10% free 3667K/4068K, paused 6ms, total 7ms
12-19 22:58:27.644: D/dalvikvm(29223): GC_FOR_ALLOC freed 241K, 11% free 3742K/4164K, paused 6ms, total 7ms
12-19 22:58:27.649: I/AwesomePlayer(13841): setDataSource_l(URL suppressed)
12-19 22:58:27.659: I/AwesomePlayer(13841): setDataSource_l(URL suppressed)
12-19 22:58:27.674: D/dalvikvm(29223): GC_CONCURRENT freed 255K, 8% free 3908K/4240K, paused 1ms+0ms, total 9ms
12-19 22:58:27.674: I/Camera2ClientBase(13841): Camera 0: Opened
12-19 22:58:27.674: D/ExynosCameraHAL2(13841): >>> I'm Samsung's CameraHAL_2(ID:0) <<<
12-19 22:58:27.674: D/ExynosCameraHAL2(13841): g_cam2_device : 0x00000000
12-19 22:58:27.674: D/ExynosCameraHAL2(13841): (ExynosCameraHWInterface2): ENTER
12-19 22:58:27.789: D/dalvikvm(29223): GC_CONCURRENT freed 210K, 6% free 4151K/4416K, paused 2ms+7ms, total 50ms
12-19 22:58:27.854: D/dalvikvm(29223): GC_CONCURRENT freed 281K, 8% free 4336K/4676K, paused 1ms+1ms, total 11ms
12-19 22:58:27.879: D/ExynosCameraHAL2(13841): (ExynosCameraHWInterface2): EXIT
12-19 22:58:27.904: D/gamook(29223): Loaded 66 configuration entries.
12-19 22:58:27.914: D/dalvikvm(29223): GC_CONCURRENT freed 399K, 10% free 4447K/4908K, paused 1ms+4ms, total 27ms
12-19 22:58:27.914: D/dalvikvm(29223): WAIT_FOR_CONCURRENT_GC blocked 1ms
12-19 22:58:27.934: D/mali_winsys(28831): new_window_surface returns 0x3000
12-19 22:58:27.964: D/ExynosCameraHAL2(13841): (allocateStream): stream width(1920) height(1080) format(22)
12-19 22:58:27.969: D/ExynosCameraHAL2(13841): (registerStreamBuffers): stream_id(0), num_buff(8), handle(b8683a48)
12-19 22:58:27.974: D/ExynosCameraHAL2(13841): (allocateStream): stream width(2560) height(1920) format(21)
12-19 22:58:28.009: D/dalvikvm(29223): GC_CONCURRENT freed 279K, 8% free 4608K/4988K, paused 2ms+2ms, total 22ms
12-19 22:58:28.039: D/ExynosCameraHAL2(13841): (registerStreamBuffers): stream_id(4), num_buff(6), handle(b85aabd8)
12-19 22:58:28.039: D/ExynosCameraHAL2(13841): (allocateStream): stream width(2560) height(1920) format(ffffffff)
12-19 22:58:28.039: D/ExynosCameraHAL2(13841): (allocateStream): jpeg stream exists
12-19 22:58:28.039: D/ExynosCameraHAL2(13841): START stream thread 1 release 1869
12-19 22:58:28.049: D/ExynosCameraHAL2(13841): END stream thread 1 release 1874
12-19 22:58:28.064: D/dalvikvm(29223): GC_CONCURRENT freed 320K, 8% free 4694K/5076K, paused 3ms+2ms, total 18ms
12-19 22:58:28.139: D/dalvikvm(29223): GC_CONCURRENT freed 452K, 10% free 4697K/5212K, paused 3ms+2ms, total 30ms
12-19 22:58:28.184: D/ExynosCameraHAL2(13841): (registerStreamBuffers): stream_id(5), num_buff(11), handle(b855dc30)
12-19 22:58:28.199: D/dalvikvm(29223): GC_CONCURRENT freed 433K, 10% free 4775K/5268K, paused 2ms+2ms, total 29ms
12-19 22:58:28.199: D/ExynosCameraHAL2(13841): (allocateReprocessStreamFromStream): output_stream_id(5)
12-19 22:58:28.249: D/dalvikvm(29223): GC_CONCURRENT freed 513K, 11% free 4828K/5400K, paused 1ms+1ms, total 16ms
12-19 22:58:28.249: D/dalvikvm(29223): WAIT_FOR_CONCURRENT_GC blocked 5ms
12-19 22:58:28.294: D/dalvikvm(29223): GC_CONCURRENT freed 528K, 11% free 4884K/5472K, paused 2ms+0ms, total 17ms
12-19 22:58:28.294: D/dalvikvm(29223): WAIT_FOR_CONCURRENT_GC blocked 3ms
12-19 22:58:28.344: D/dalvikvm(29223): GC_CONCURRENT freed 601K, 12% free 4888K/5548K, paused 2ms+1ms, total 19ms
12-19 22:58:28.344: D/dalvikvm(29223): WAIT_FOR_CONCURRENT_GC blocked 7ms
12-19 22:58:28.389: D/dalvikvm(29223): GC_CONCURRENT freed 616K, 13% free 4857K/5548K, paused 0ms+1ms, total 17ms
12-19 22:58:28.399: D/ExynosCameraHAL2(13841): ### Applying AF Mode change(Mode 2)
12-19 22:58:28.404: D/dalvikvm(29223): GC_FOR_ALLOC freed 17K, 13% free 4843K/5548K, paused 14ms, total 14ms
12-19 22:58:28.404: I/dalvikvm-heap(29223): Grow heap (frag case) to 4.823MB for 39033-byte allocation
12-19 22:58:28.419: D/dalvikvm(29223): GC_FOR_ALLOC freed <1K, 13% free 4881K/5588K, paused 15ms, total 15ms
12-19 22:58:28.434: D/dalvikvm(29223): GC_FOR_ALLOC freed <1K, 13% free 4881K/5588K, paused 19ms, total 19ms
12-19 22:58:28.439: I/dalvikvm-heap(29223): Grow heap (frag case) to 4.897MB for 78050-byte allocation
12-19 22:58:28.454: D/ExynosCameraHAL2(13841): DEBUG(m_mainThreadFunc)(0x10): No more service requests left in the queue
12-19 22:58:28.459: D/dalvikvm(29223): GC_FOR_ALLOC freed 0K, 13% free 4957K/5668K, paused 24ms, total 24ms
12-19 22:58:28.529: D/dalvikvm(29138): GC_CONCURRENT freed 359K, 11% free 3745K/4168K, paused 1ms+1ms, total 13ms
12-19 22:58:28.559: D/dalvikvm(29113): GC_CONCURRENT freed 378K, 11% free 3991K/4436K, paused 2ms+1ms, total 14ms
12-19 22:58:28.634: D/dalvikvm(29223): GC_CONCURRENT freed 463K, 12% free 5023K/5668K, paused 3ms+2ms, total 20ms
12-19 22:58:28.639: I/Choreographer(28831): Skipped 43 frames! The application may be doing too much work on its main thread.
12-19 22:58:28.704: I/ActivityManager(447): Process com.google.android.apps.plus (pid 28888) has died.
12-19 22:58:28.719: I/ActivityManager(447): Process com.google.process.gapps (pid 28963) has died.
12-19 22:58:28.734: I/ActivityManager(447): Displayed com.example.dif/.CameraActivity: +1s138ms
12-19 22:58:28.804: I/ActivityManager(447): Process com.facebook.katana:dash (pid 29014) has died.
12-19 22:58:28.864: I/ActivityManager(447): Start proc com.estrongs.android.pop for broadcast com.estrongs.android.pop/.app.InstallMonitorReceiver: pid=29263 uid=10096 gids={50096, 3003, 3002, 3001, 1028, 1015}
12-19 22:58:28.989: W/System.err(29263): java.lang.NoSuchMethodException: setCompatibilityInfo [class android.content.res.CompatibilityInfo$1]
12-19 22:58:28.994: W/System.err(29263): at java.lang.Class.getConstructorOrMethod(Class.java:472)
12-19 22:58:28.994: W/System.err(29263): at java.lang.Class.getMethod(Class.java:857)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.util.af.a(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.util.af.a(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.pop.esclasses.i.<init>(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.pop.esclasses.i.a(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.pop.FexApplication.getResources(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.ui.b.a.a(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at com.estrongs.android.pop.FexApplication.onCreate(Unknown Source)
12-19 22:58:28.994: W/System.err(29263): at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1007)
12-19 22:58:28.994: W/System.err(29263): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4344)
12-19 22:58:28.994: W/System.err(29263): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
12-19 22:58:28.994: W/System.err(29263): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-19 22:58:28.994: W/System.err(29263): at android.os.Handler.dispatchMessage(Handler.java:102)
12-19 22:58:28.994: W/System.err(29263): at android.os.Looper.loop(Looper.java:136)
12-19 22:58:28.994: W/System.err(29263): at android.app.ActivityThread.main(ActivityThread.java:5017)
12-19 22:58:28.994: W/System.err(29263): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 22:58:28.994: W/System.err(29263): at java.lang.reflect.Method.invoke(Method.java:515)
12-19 22:58:28.994: W/System.err(29263): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-19 22:58:28.994: W/System.err(29263): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-19 22:58:28.994: W/System.err(29263): at dalvik.system.NativeStart.main(Native Method)
12-19 22:58:28.994: E/ObjectHelper(29263): Can't find method:setCompatibilityInfo
12-19 22:58:29.034: I/ActivityManager(447): Process com.google.android.gms (pid 29003) has died.
12-19 22:58:29.044: D/dalvikvm(29263): GC_CONCURRENT freed 244K, 9% free 3311K/3616K, paused 2ms+1ms, total 18ms
12-19 22:58:29.059: I/ActivityManager(447): START u0 {flg=0x10800000 cmp=com.estrongs.android.pop/.app.InstallMonitorActivity (has extras)} from pid 29263
12-19 22:58:29.104: I/ActivityManager(447): Start proc com.metago.astro for broadcast com.metago.astro/com.kii.cloud.collector.Receiver: pid=29277 uid=10085 gids={50085, 3003, 1028, 1015}
12-19 22:58:29.174: D/Print(28831): resume()
Fragment is getting replace twice that's why onResume of Fragment getting called twice. You should call your RefreshFragment() only from onCreate() of activity and need not be called from onResume() of activity. ok, scroll saving is working now after I commented RefreshFragment() in activity's onResume().
If the user opens Settings-Activity and goes back again to MainActivity, onResume and onPause get called three times. This increases each time the user opens Settings-Activity and going back to MainActivity.
onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() .
4. onResume() is called whenever you navigate back to the activity from a call or something else. You can override the onResume method similarly as onCreate() and perform the task.
I have found out that this- Where is this toast coming from? causes the camera preview to refresh again.
It is the ES File Explorer app that generates an "Internal Storage Space left" toast which, I guess pauses the Camera preview for a brief moment and the onResume() has to be called again. Disabling that toast message no longer causes the pause and refresh on the Preview.
Thanks everyone for helping. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With