I am building a camera app everything works file when i put it to the background but when i explicitly press the lock button it does not work,i am not finding and difference between the callback methods being called or am i missing something please help.
Put log line inside these methods
onPause
onSaveInstanceState
onStop
onDestroy
onResume
onRestoreInstanceState
onStart
onCreate
And make sure the same methods are called in both situation.
Related
I am stuck in a strange situation....
I have declared android:configChanges="orientation|keyboardHidden" in the manifest file of the Activity. So, ideally I want my Activity onConfigurationChanged() to be called whenever, I rotate the device. But, that does not happen.
Inspite of having these attributes in the manifest file, the Activity onStop() is called first followed by onCreate, (the onConfigurationChanged() is never called) when I rotate the device ----- This is the issue I am facing.
My expectation is --- Whenever, I rotate the device, onConfigurationChanged () to be called first, then onStop() and then onCreate().
What is the mistake that I am doing?
Is this at all possible ?
No, not possible.
You put android:configChanges="keyboardHidden|orientation|screenSize"in your manifest to handle configuration changes yourself.
You override the onConfigurationChanged() method and take care of configuration changes there.
onStop() and onCreate() are never called again, only onConfigurationChanged() is called during a configuration change in this case because the activity is never killed.
When I exit my app (by pressing back or the home button) the Activitys onDestroy() method is called (where I do lots of clean up with bitmaps).
When I reopen the app, onCreate() does not get called... it goes straight to onStart(), despite the fact that the Activity was finished. This is causing a "trying to use a recycled bitmap" error.
Is there a way to ensure that onCreate() is always called after an Activity is destroyed?
EDIT: I was mistaken. onCreate() IS being called. However, I am still getting the "trying to use a recycled bitmap" error. If onCreate() is going through all of it's steps, wouldn't any recycled bitmaps be reloaded?
Your app must be doing something to forcefully ensure that onDestroy gets called, because if you look at the Activity lifecycle there's no path to get back to onStart from onDestroy that doesn't include onCreate. In reality, an Activity unwinds its initialization with reverse callbacks to the ones that bring it into the resumed state. Take a look at the official documentation here Perhaps you're calling the finish() method somewhere to force quite the Activity?
when you press the home button , the activity is not destroyed , it is just sent to background , and the method onPause() is called, and when you launch it again , the method onResume() will be executed, the method onDestroy() is executed when you press the back button or when you call the method finish() to force the activity to be destroyed , and then when you try to re launch your activity , the onCreate() will be executed.
refer this
The problem was with how I was setting the ImageView image. My original way to load an image from /res was:
image.setImageDrawable(getResources().getDrawable(R.drawable.myImage)); //WRONG!!!!
apparently if you recycled a bitmap, the above code will not reallocate memory for the bitmap, and your program will crash when it tries to draw that ImageView.
The correct way to load a bitmap that has been recycled (or at least, the way that solved my problem) is:
image.setImageDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.myImage))); //correct!
It still doesn't answer my question as to why, after exiting the app, and onDestroy is called, that when I re-enter the app, it is looking for a recycled bitmap. In theory the app should be starting up from scratch.
It looks like anything that occurs in onResume() wont have to be placed in onCreate since onResume() is always run after onCreate()? I'm asking this because i have an algorithm that must be executed every time a user comes back to the main activity. If it is already in onResume() then do i have to have it in onCreate()?
No, you don't need it. onResume is always called after onCreate so if onCreate runs so will onResume.
You are correct. onResume is always called after onCreate.
Basically so. but you need to think what pause/resume and start/stop mean for your app. Also when you are in OnCreate, the activity is not yet created, where as in onStart is it. This may have impact in properties of the activity that you seek to use/change.
I'm trying to get the result from getLocationOnScreen() as early as possible.
However, putting it in the onCreate() event I'll get 0 as my results (which I suspect is because the Window has not been fully created). onResume() also does not work unless the application has been brought to background and then front again (i.e. it works on the second call of onResume()).
Where should I put getLocationOnScreen() so I can get the results early; or maybe is there a way to force getLocationOnScreen() to work even before the Window is fully created?
Thanks.
The activity lifecycle is
onCreate
onStart
onResume
//app is running
onPause
onStop
onDestroy
All of which you can override
http://developer.android.com/guide/topics/fundamentals/activities.html#ImplementingLifecycleCallbacks
I am trying to save data in my activity and than restore it.
I save data in on onSaveInstanceState() and then I try to restore the data in onRestoreInstanceState().
I setup breakpoint, the method onSaveInstanceState() get called. But onRestoreInstanceState() or onCreate() never did.
Here is my steps:
start my Activity.
press 'Home' button on the phone. onSaveInstanceState() get called.
Click the icon in the launcher and launch my Activity again.
At this time, only onRestart() get called. But not onRestoreInstanceState() or onCreate().
Does anyone know why?
Well, if onRestart() is called, the value of the instance variables would be maintained by the application stack itself and thus you do not need to restore them.
onCreate() method is only called when your Activity's onStop() is called and the process is killed.
Please refer the Activity life cycle Android Activity Life Cycle for a clear understanding.
You may want to check whether the onStop() method is called and if your process is killed. I do no think that your process gets killed by the scenario which you have described.
the onRestoreInstanceState() method is very tricky. I do not know when exactly it is called but I saw it was called once while changing from Potrait to Landscape.
From doc:
The system calls onRestoreInstanceState() only if there is a saved state to restore.
I have asked similiar question earlier on here
Here's some steps to test out onRestoreInstanceState():
Press home screen
Kill the app through adb
Launch your app again
Follow these steps (Using Android Studio):
Create New Logcat Filter, e.g. AppState
Launch the app on your emulator. You will see:
I/AppState﹕ onCreate
I/AppState﹕ onStart
I/AppState﹕ onResume
Press Ctl-F12 to rotate the emulator. You will see:
I/StateChange﹕ onPause
I/StateChange﹕ onSaveInstanceState
I/StateChange﹕ onStop
I/StateChange﹕ onDestroy
I/StateChange﹕ onCreate
I/StateChange﹕ onStart
I/StateChange﹕ onRestoreInstanceState
I/StateChange﹕ onResume
This causes the destruction and recreation of the activity by making a configuration change to the device, such as rotating from portrait to landscape.
See the link below for how to test onSaveInstanceState() and onRestoreInstanceState() on a real device or in the emulator.
This method uses the AlwaysFinish setting, which is simpler and faster than killing processes. This method also provides Activity-level control rather than process level control:
http://bricolsoftconsulting.com/how-to-test-onsaveinstancestate-and-onrestoreinstancestate-on-a-real-device/
This is my solution for real device so onRestoreInstanceState get it called.
in manifest on related activity remove this part android:configChanges="orientation"
in manifest on related activity remove this partandroid:screenOrientation="portrait"
in your activity remove this line if it's there setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
on your device enable rotating from setting - display - auto rotate.
then run your app, rotate your device.
that's it.