weixin_39968820
2021-01-12 18:39 阅读 1

FbDialog disappears and/or crashes when the orientation changes (task #467311)

When Fb dialog is displayed (to display the Facebook login web page) either in Portrait or Landscape orientation, it disappears as soon as the orientation of the phone changes.

该提问来源于开源项目:facebook/facebook-android-sdk

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

26条回答 默认 最新

  • weixin_39968820 weixin_39968820 2021-01-12 18:39

    Moreover it often gets the application to crash :\

    点赞 评论 复制链接分享
  • weixin_39584549 weixin_39584549 2021-01-12 18:39

    Ya, this is a known issue, but it appears to be a non-trivial change to correct. If you have a simple fix, please send me a code diff and I will take a look; otherwise, sorry, I don't have an ETA for a fix. In the meantime, you can simply prevent your app from rotating by adding

    android:screenOrientation="portrait"

    as a property of your activity in the application manifest.

    点赞 评论 复制链接分享
  • weixin_39594457 weixin_39594457 2021-01-12 18:39

    soneff, try to use setOwnerActivity() on the dialog itself. This should wire it up to the context and maintain the dialog ;)

    点赞 评论 复制链接分享
  • weixin_39594457 weixin_39594457 2021-01-12 18:39

    http://developer.android.com/guide/topics/ui/dialogs.html

    点赞 评论 复制链接分享
  • weixin_39719077 weixin_39719077 2021-01-12 18:39

    Hello,

    Add this code in the manifest file for the Example activity. This will retrain the dialog when the orientation is changed.

    android:configChanges="keyboardHidden|orientation"

    点赞 评论 复制链接分享
  • weixin_39628247 weixin_39628247 2021-01-12 18:39

    This patch will at least act as a bandaid over the crash. I too would like to see a solution to the overall problem that doesn't involve manually handling configchanges

    
    diff -r 44b3f51dfe41 facebook/src/com/facebook/android/FbDialog.java
    --- a/facebook/src/com/facebook/android/FbDialog.java   Wed Jul 14 15:52:05 2010 -0400
    +++ b/facebook/src/com/facebook/android/FbDialog.java   Wed Jul 14 16:18:27 2010 -0400
    @@ -162,7 +162,11 @@
                 if (title != null && title.length() > 0) {
                     mTitle.setText(title);
                 }
    -            mSpinner.dismiss();
    +            try {
    +                mSpinner.dismiss();
    +            } catch( IllegalArgumentException ignored ) {
    +                // thrown if the underlying activity was dismissed, eg. during device orientation change
    +            }
             }   
    
         }
    
    点赞 评论 复制链接分享
  • weixin_39760721 weixin_39760721 2021-01-12 18:39

    This solution:

    android:configChanges="keyboardHidden|orientation"

    Doesn't quite work well. In my case I attach a picture to the post and that picture makes the dialog taller. When rotating the screen when this option is activated, the buttons "publish" and "skip" disappear from the screen and the user cannot press them anymore, the only solution is to rotate the screen back. The dialog does not scroll.

    This problem does not occur when the configChanges setting is not set (but then it crashes when the user rotates the screen too much...). And the layout is OK before rotation (OK when the phone is held vertically, OK when held horizontally, the problem comes after rotation).

    The problem with not supporting rotation is that I must turn it off for my whole activity, it seems I can't do it for just this dialog. And the rest of that activity profits from screen rotation.

    Also the try/catch workaround from emmby is not quite enough. The other dismiss at around line 127 still triggers the crash even after that try/catch is added. So I guess try/catch must be added in still other locations.

    点赞 评论 复制链接分享
  • weixin_39628247 weixin_39628247 2021-01-12 18:39

    What's the status on this? The library consistently still crashes whenever a user changes their orientation.

    点赞 评论 复制链接分享
  • weixin_39769183 weixin_39769183 2021-01-12 18:39

    I've fixed dialog rotation in my fork of facebook-android-sdk: http://github.com/Pretz/facebook-android-sdk

    To fix it I had to change how you show the dialog; you'll need to use Activity.showDialog() and create the dialog in a separate step. I updated the simple example to do this.

    点赞 评论 复制链接分享
  • weixin_39611930 weixin_39611930 2021-01-12 18:39

    Pretz,

    I tried out your fix and the change to how you display the dialog and it worked correctly. Thanks for the fix!

    Are there are lot of changes in your fork that might affect calls to the Facebook graph api, or are your changes simply fixing display issues? I'm trying to get an idea of how "safe" it is to take your branch or how much testing i should do with it.

    Also, we should get that fix back in the official master!

    点赞 评论 复制链接分享
  • weixin_39769183 weixin_39769183 2021-01-12 18:39

    jaredbro, I tried to keep my changes as minimal as possible. While it did require significantly changing how the dialog is displayed, I didn't touch any of the logic concerning the Graph API.

    I've already sent a pull request to facebook; hopefully my changes can be integrated soon.

    Cheers!

    点赞 评论 复制链接分享
  • weixin_39789979 weixin_39789979 2021-01-12 18:39

    Just having a play with Pretz branch because of the dialog issues it says its fixed. The main difference that I can see is that rather than trying to keep the dialog open and reload it when orientation it just dismisses it, making the user have to click the button again.

    点赞 评论 复制链接分享
  • weixin_39789979 weixin_39789979 2021-01-12 18:39

    The Pretz branch fixes the problem if you wait for the content to finish loading but if you change orientation before the content has finished loading it leaks memory and crashes still.

    点赞 评论 复制链接分享
  • weixin_39769183 weixin_39769183 2021-01-12 18:39

    I haven't been able to reproduce a crash while the webview is loading. Can you describe exactly what you did for that to happen? How did you show the dialog in the first place?

    点赞 评论 复制链接分享
  • weixin_39789979 weixin_39789979 2021-01-12 18:39

    I was showing the dialog incorrectly, followed your example code and it works well. Deleting my previous post about the download part as its misleading

    点赞 评论 复制链接分享
  • weixin_39724382 weixin_39724382 2021-01-12 18:39

    Really, the FbDialog code should not be written as a Dialog, but rather an Activity with Theme.Dialog applied to it. Then FB can achieve the partial screen coverage appearance they desire. but allowing the view to manage it's own state during rotation. Regardless, views that interact with a keyboard but do not support rotation might as well not exist because that is the ultimate UX fail on Android. Please fix this ASAP, or pull Pretz's fork.

    点赞 评论 复制链接分享
  • weixin_39769183 weixin_39769183 2021-01-12 18:39

    I agree. Unfortunately, I can no longer recommend my fork as it doesn't support instant connect through the Facebook app, and I don't plan on updating it to the latest facebook SDK features.

    Until Facebook provides a better solution, I recommend having any activity that can launch fbDialog declare android:configChanges="keyboardHidden|orientation" in the manifest, which will keep the dialog on screen when the orientation changes. Unfortunately, if your activity depends on different resources for landscape/portrait, this could require a certain amount of work

    点赞 评论 复制链接分享
  • weixin_39613712 weixin_39613712 2021-01-12 18:39

    Facebook: please develop a work around for this. Many Android devices have slide out keyboards and/or users that like to type in landscape mode. Forcing an app to stay in portrait just to avoid force close issues makes for a poor user experience.

    点赞 评论 复制链接分享
  • weixin_39531229 weixin_39531229 2021-01-12 18:39

    This took me a while but I solved this in a similar fashion, although it required more than just setting the configChanges="orientation". Just adding that would keep the dialog on screen but it would keep it in its current orientation. In other words if you were in landscape with a landscape dialog and switched to portrait you would end up in portrait with a landscape dialog. Almost as bad as the crashing.

    So, I did the following: - added a redraw() method to FbDialog that would redraw the dialog on request - in whatever activity I wanted to show the dialog I added the configChange="orientation" and in the onConfigurationChanged call I called the redraw() method.

    This seems to work and have had no more reports of crashing in my app. I did not have to change anything else about how I showed the dialog or was using the API. In addition to that I added a redraw() method to redraw the dialog

    点赞 评论 复制链接分享
  • weixin_39613712 weixin_39613712 2021-01-12 18:39

    Thx sroorda. That could work. Can you post your FbDialog.redraw method?

    点赞 评论 复制链接分享
  • weixin_39954674 weixin_39954674 2021-01-12 18:39

    I have one suggestion. It would be better if the dialog had the same layout for both orientations. Then the solution with android:configChanges="keyboardHidden|orientation" will work well. I think, it is the best solution for now, since it doesn't crash on orientation changes, the dialog looks nice for any display and any orientation, and it saves its state on orientation changes(if user has already entered his login or password) technical details: mWebView should use WRAP_CONTENT layout. mContent should use WRAP_CONTENT or FILL_PARENT layout. it looks better, if subviews added to dialog when webView has already loaded. Pretz, your solution more correctly, waiting for it! but in this case it also be better to have one layout for different orientations(in case of an activity doesn't recreates on rotation),as I suggest upper

    点赞 评论 复制链接分享
  • weixin_39628271 weixin_39628271 2021-01-12 18:39

    Has there been any progress made on this issue?

    点赞 评论 复制链接分享
  • weixin_39814482 weixin_39814482 2021-01-12 18:39

    reading this thread makes me feel embarraced for facebook.

    点赞 评论 复制链接分享
  • weixin_39629876 weixin_39629876 2021-01-12 18:39

    Any news on this issue? It's unacceptable

    点赞 评论 复制链接分享
  • weixin_39998521 weixin_39998521 2021-01-12 18:39

    I had a same issue when I use android:configChanges="keyboardHidden|orientation To solve this, I modified FbDialog.java.

    --mContent = new LinearLayout(getContext()); ++mContent = new ConfigChangeLinearLayout(getContext(), this);

    ConfigChangeLinearLayout is a custom LinearLayout which resizes when configuration changed.

    private static class ConfigChangeLinearLayout extends LinearLayout { private Dialog mDialog; public ConfigChangeLinearLayout(Context context, Dialog dialog) { super(context); mDialog = dialog; }

    
    
        protected void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
    
            Display display = mDialog.getWindow().getWindowManager().getDefaultDisplay();
            final float scale =
                getContext().getResources().getDisplayMetrics().density;
            float[] dimensions =
                (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
                        ? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT;
            setLayoutParams(new FrameLayout.LayoutParams(
                    display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)),
                    display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));         
        }
    }
    
    点赞 评论 复制链接分享
  • weixin_39802969 weixin_39802969 2021-01-12 18:39

    This issue was raised against an previous version of the Facebook SDK for Android.

    In 2012, the SDK had a significant rewrite and relaunch, and we are closing issues and pull requests that predate that v3.0 release.

    If you are still experiencing this issue, please raise a new issue with repro steps in the supported SDK (currently v3.6). For more information, please see our Android developer center at https://developers.facebook.com/docs/android.

    Many thanks for using the Facebook Platform, and your support of this project.

    点赞 评论 复制链接分享

相关推荐