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
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));
}
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).
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