Replacing fragments and orientation change

前端 未结 3 1489
余生分开走
余生分开走 2020-12-25 14:20

I\'m developing an Android application targeting 2.x and 3.0 devices and thus I\'m using the compatibilty API. I\'m testing on Android 2.0.

I\'m trying to replace a

相关标签:
3条回答
  • 2020-12-25 14:43

    After struggling with this for many hours, I finally got it. The problem is not in the setup or the way we call Fragment class at all. It has to do with a wrong message being displayed on the screen. If you have a check for a container being null in your onCreateView() in your Ftagment class, you will get that "unable to inflate fragment" message, instead of container is null message. So, do not check for null container in your onCreateView(). So make sure your onCreateView() looks like this:

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return(inflater.inflate(R.layout.title_layout, container, false));
        }
    

    and NOT like this:

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            if(container == null){
                return null;                                
            }
            return(inflater.inflate(R.layout.title_layout, container, false));
        }
    
    0 讨论(0)
  • 2020-12-25 14:45

    For reference reasons: We were able to solve a very similar problem by adding an android:id to our fragment created in XML.

    This very important precondition for supporting crash-free rotation is only mentioned as a side-note in the documentation:

    Note: Each fragment requires a unique identifier that the system can use to restore the fragment if the activity is restarted (and which you can use to capture the fragment to perform transactions, such as remove it).

    0 讨论(0)
  • 2020-12-25 14:49

    Don't create the SearchFormFragment in XML. Instead have an empty FrameLayout which you populate in Activity.onCreate() without adding it to the back stack. This way the Activity will keep the current Fragment instead of trying to add the one specified in XML.

    Also, using AsyncLoader may be a better approach, see http://code.google.com/p/android/issues/detail?id=14944

    0 讨论(0)
提交回复
热议问题