Solve the problem that the calling system camera Activity is destroyed

The system camera needs to be enabled in the project to take pictures. An easy question. But there is a problem in the adaptation.

Briefly state the question:

    Some mobile phones take pictures successfully, and some mobile phones determine that the activity data is lost and destroyed after returning after taking pictures.

Problem finding:

    After code search, it was found that when these problematic mobile phones were called by the system mobile phone to take pictures, the current activity was destroyed and then re-created. That is, Acitivity executes ondestory()-->onCreate(), so the Activity is recreated and the internal data is lost, and according to the normal logic, the Activity should enter the background stack. At first I thought it was because the system enabled the camera memory to be automatically destroyed and recycled. But some phones do not have this problem. Very helpless.

    After I carefully studied the life cycle of the activity, I suddenly realized that the system will recreate an activity when the mobile phone is horizontal and vertical, which is why the last activity will be ondestory()-->onCreate(). But why some phones will perform and some won't. The problem lies in the horizontal and vertical screen. Because the system camera of some mobile phones is very dazzling, it will automatically rotate 90 degrees, which leads to the occurrence of horizontal and vertical screens (--! It seems that the most trendy is not a good thing, it is hard for us code farmers). That's why some phones happen and some don't. It is related to whether the system camera is rotated.

    Finally post the solution.

    AndroidMainifest.xml added

      <activity
            android:name=".Activity"
            android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" />
        <activity

    Override acitivity's onConfigurationChanged method

   @Override
    public void onConfigurationChanged(Configuration newConfig) {

      Actually do nothing here
    super.onConfigurationChanged(newConfig);
    }

 

If it is a relatively low version, this can be done. If a higher version may need to add other attributes to trigger onConfigurationChanged, that is, android:configChanges="orientation|keyboardHidden|screenSize"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325183564&siteId=291194637