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.
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 inonCreate(). (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 theBundlepassed toonCreate()(which is called again after device rotation) instead. ThatBundlewill contain everything that was stored inonSaveInstanceState().If you need to postpone your work until
onResume(), you’ll want to useonCreate()to populate some member variables in yourActivityclass, and then make use of those variables inonResume().