4

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.

vinothp
  • 9,939
  • 19
  • 61
  • 103
  • Regarding this forced switch to landscape, there is no good news for you in my cookbook. The camera app runs in landscape, and the Android system sometimes is not quick enough to follow your activitiy's reqest to run portrait only. Tha's why I use landscape orientation for activities that may call camera intent, even if it means that I must fake the portrait orientation myself. – Alex Cohn Oct 29 '12 at 15:56

1 Answers1

2

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().

Community
  • 1
  • 1
acj
  • 4,821
  • 4
  • 34
  • 49
  • Thanks for your detailed answer. I will try those and come back to you – vinothp Oct 29 '12 at 12:56
  • I tried your answer but the onrestoreInstancestate is never get called. And also is the any possible way to call onsavedinsatncestate in onresume.. Thanks – vinothp Oct 29 '12 at 14:32
  • Did you mean "call `onRestoreInstanceState` in `onResume`"? You shouldn't need to call `onRestoreInstanceState` or `onSaveInstanceState` from `onResume`. I've expanded my answer to address your questions. – acj Oct 29 '12 at 14:47
  • `onSaveInstanceState` gives you a `Bundle`, and you'll store your state information in there. After the activity restarts, `onCreate()` will be called again, and it will give you the same `Bundle` back. If it's not `null`, then you can store the values in the `Bundle` into member variables and use them wherever you see fit -- in `onResume()` or elsewhere. – acj Oct 29 '12 at 14:55
  • Yeah. The problem is here. The `onCreate()` is not called instead of that `onResume()` gets called. – vinothp Oct 29 '12 at 15:00
  • If I understand correctly, you're launching the Camera from your app, returning to your app after taking a photo (or pressing the Back button), and the spinner values are reset. If only `onResume()` (and not `onCreate()`/`onStart()`) is being called, then your spinners should be unaffected. Is your `onResume()` code overwriting the old spinner values, maybe? If so, initializing the spinners in `onCreate()` might be a better approach. – acj Oct 29 '12 at 15:06
  • 1
    It's also possible to save any UI related values (e.g. spinner states) in `onPause()` and use these values in `onRestore()`. You can use activity object members to store this information. The instance will not be destroyed before `onSaveInstanceState()` gets called. – Alex Cohn Oct 29 '12 at 15:34
  • @acj you are correct. But what i am doing is my second spinner values are based on my first spinner selected value. Thats why in onResume it resets the second spinner some times first spinner as well in `Samsung Galaxy 3 running 4.0.3`. – vinothp Oct 29 '12 at 17:08
  • @Venture There are two separate issues here. Alex Cohn's suggestion to save state in `onPause()` and restore it in `onResume()` will fix the case where only the second spinner is reset (by your own code). It will also allow the user to press the Home button and return to your activity. Second issue: If the first spinner is also reset, it probably means that the activity was destroyed and recreated (most likely due to device rotation), which can be handled with the solution that I gave in my answer. – acj Oct 29 '12 at 17:46
  • 1
    acj and @Alex Cohn thanks for your help guys. It works perfectly now. Cheers +1 – vinothp Oct 30 '12 at 12:11