简体   繁体   中英

android: application crashes if orientation changes in a subactivity

My main activity handles changes into portrait mode just fine. However, if I launch a very simple activity via startActivityForResult() and switch to portrait mode before finishing it, my main activity crashes because some of its variables are set to null.

It looks like my connection to the Service is not being recreated. I tried manually calling doUnbindService(); doBindService(); doUnbindService(); doBindService(); inside of onActivityResult() but onServiceConnected() is still not being called.

What's the proper way to handle the Activity lifecycle here?

EDIT : I see in the log file that onServiceConnected() is indeed being called -- but it's being called after onActivityResult() .

For you second Activity add this

  if your android:targetSdkVersion="12" or less
     android:configChanges="orientation|keyboardHidden">

      if your  android:targetSdkVersion="13" or more
  android:configChanges="orientation|keyboardHidden|screenSize">

in manifest.xml

The problem was that I was referring to objects which don't exist until onCreate() is called again. I needed to queue my answers locally and wait for those objects to be recreated to avoid crashes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM