Reputation: 10059
I am developing an app with capturing the image as one of the feature. In my home screen i have two spinner. After selecting spinner values user can go capturing the picture by clicking take picture button. Up to this working fine. But the problem resides in Camera App Orientation.
Take Picture button launches the camera.When image get captured it saved(absolutely fine) and came back to the parent activity. But the problems is it refreshes the activity. while coming back i can see following wired things
1.)Some times it shows landscape screen(1second) and back to portait which refreshes the activity and results in resetting the Spinner values.
2.) Some times it just resets the Spinner values.
It's really annoying. I haven't got any clue to get rid this problem. I hope some of you guys will solve this.
Much Appreciated.
Upvotes: 4
Views: 1286
Reputation: 4821
It's good to be prepared for anything when you're launching an activity in another app (Camera). The activity could return bad data, the screen orientation could be changed, or it could be a very long time before the user returns to your app. From your description, it sounds as though the orientation is changing every time the Camera app is launched.
Android has built-in state management methods to handle this type of scenario. You can override the onSaveInstanceState()
method to store your activity's state (e.g., spinner values), and then restore that state in onCreate()
. (Example here.) This will handle the case in which you're launching the Camera app, or your user presses the Home button and then returns to the app, etc.
EDIT:
onRestoreInstanceState()
is only called if your activity is killed due to memory pressure and then recreated later. It's much more reliable to use the Bundle
passed to onCreate()
(which is called again after device rotation) instead. That Bundle
will contain everything that was stored in onSaveInstanceState()
.
If you need to postpone your work until onResume()
, you'll want to use onCreate()
to populate some member variables in your Activity
class, and then make use of those variables in onResume()
.
Upvotes: 2