Orientation of camera app updates parent activity?

I am developing an image capture application as one of the features. In my home screen I have two counters. After selecting the counter values, the user can capture the image by clicking the "Capture Image" button. Until this working fine. But the problem is with the orientation of the camera.

The "Capture" button launches the camera. When the image is captured, it is saved (absolutely normal) and returns to parental activity. But the problem is that it is updating activity . returning, I see the following wired things.

1.) Several times it displays a landscape screen (1 second) and back to portait, which updates the activity and resets the Spinner.

2.) Several times, it simply resets the Spinner.

This is really annoying. I have no clue to get rid of this problem. I hope some of you guys solve this.

Significant appreciation.

+4
source share
1 answer

It’s good to be prepared for everything when you start activity in another application (Camera). Activity can return bad data, the screen orientation can be changed, or it can be very long before the user returns to your application. From your description, it sounds as if the orientation changes each time you start the Camera application.

Android has built-in state management methods for working with this type of script. You can override the onSaveInstanceState() method to save the activity state (for example, counter values), and then restore this state to onCreate() . (An example is here .) This will handle the case when you launch the Camera application, or the user clicks the Home button, and then returns to the application, etc.

EDIT

onRestoreInstanceState() is called only if your activity is killed due to memory pressure and then recreated later. It is much more reliable to use the Bundle passed to onCreate() (which is called again after the device rotates). This Bundle will contain everything that has been stored in onSaveInstanceState() .

If you need to defer your work to onResume() , you will want to use onCreate() to populate some member variables in your Activity class, and then use these variables in onResume() .

+2
source

Source: https://habr.com/ru/post/1442734/


All Articles