开发者

Android orientation changing exceptions

开发者 https://www.devze.com 2023-03-09 12:28 出处:网络
I\'m having a hard time with the screen orientation in Android. I have an a开发者_JS百科ctivity which captures a signature after the user draws his signature on the device. And for that activity I am

I'm having a hard time with the screen orientation in Android. I have an a开发者_JS百科ctivity which captures a signature after the user draws his signature on the device. And for that activity I am passing a parceable object and I get it in oncreate. When the activity changes the orientation sometimes the activity cannot get the parceable object and gives an exception. I tried using a static object for the parceable object but no use. I tried onsaveinstance state method and onRetainNonConfigurationInstance methods as well.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signature_capture_view);
    Bundle bundle = getIntent().getExtras(); 
    info = bundle.getParcelable("info");
    logo= (Bitmap) bundle.get("logo");
}

And my save instance state method is like this;

@Override
 protected void onSaveInstanceState(Bundle outState) {
 outState.putParcelable("info", info);
 super.onSaveInstanceState(outState);
 }

From the onconfiguration changed method i call the setcontentview method to set a fresh layout.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    setContentView(R.layout.signature_capture_view);
    super.onConfigurationChanged(newConfig);
}

Sometimes when the orientation changes an out of memory exception occurs from the setcontentview method.

Any help would be greatly appreciated. Thanks in advance.


Try to set the below android:configChanges attributes inside the AndroidManifest.xml file:

  <activity android:name=".MyActivity"
                  android:label="@string/app_name"
                  android:configChanges="orientation|keyboardHidden">

And if you wants that your application should be fixed to open either in Portrait or in Landscape mode then add android:screenOrientation inside the activity tag:

   <activity android:name=".MyActivity"
              android:label="@string/app_name"
              android:configChanges="orientation|keyboardHidden"
              android:screenOrientation="portrait">


if you are changing the View in landscape / portrait then you should follow these steps to resolve the out of memory error.

  1. set this in the manifest on the activity tag

android:configChanges="orientation"

  1. in Anctivity put

public void onConfigurationChanged(Configuration newConfig) {

    super.onConfigurationChanged(newConfig);

    this.recreate();
}


Try to set this in the manifest on the activity tag for the out of memory error:

android:configChanges="orientation"


I finally found the answer to my out of memory, In my signature drawing view I did not check the bitmap in the view for null and everytime the device changes the orientation it creates the bitmap inside the view. And it generates the OOM. So i inserted a null check for the bitmap and the oom disappeared. Thanks for the quick replys guys.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号