Android: Workaround for support.v4.app.Fragment & # 8594; Fragment classcastexception?

I am trying to add a support.v4.app.Fragment header to PreferenceActivity , for example:

 <header android:fragment="com.example.SupportFragmentSubClass" android:title="Selecting this should show the accompanying fragment" > </header> 

This throws a ClassCastException, presumably because the PreferenceActivity expects a subclass of the android.app.Fragment class, and not support.v4.app.Fragment .

My usage example:
I have a non-standard fragment that I want to use as a preference both on devices and on 3.0 and 3.0. For> = 3.0, I need a subclass of android.app.Fragment , so it can be embedded in the โ€œdetailed areaโ€ of preference activity on tablet devices. For <3.0, I need a subclass of v4.support.app.Fragment , so I can add an ActivityFragment to it.

Is there a workaround that would allow me to use the compatibility fragment in this situation?

+6
source share
2 answers

PreferenceFragment not included in the Android support package, and you cannot use the Fragment class to support Android in PreferenceActivity in this way. Moreover, your headers will not work at all on Android 2.x, since the PreferenceActivity in Android 2.x does not know about fragments.

Basically, you can fork PreferenceActivity from source code to create one that uses the Android Fragment support version.

Or organize your preferences for using snippets on Android 3.0+ and avoid them on Android 2.x. Here is an example project where I demonstrate a way to do this.

+7
source

As @CommonsWare points out, it's impossible that I wanted without rewriting PreferenceActivity, and that seems like a load.

The not-so-elegant solution I settled on was to create two PreferenceActivities ( as shown here ), as well as create two subclasses of Fragment, one for each fragrance of the Fragment.

So, PrefsActivityHC adds this header:

 <header <!-- An android.app.Fragment subclass --> android:fragment="com.example.project.MyFragmentHC" </header> 

... while PrefsActivity adds this preference:

 <Preference> <intent <!-- A v4.support.app.Fragment subclass, wrapped in an ActivityFragment --> android:targetClass="com.example.project.MyFragmentActivity" android:targetPackage="com.example.project" > </intent> </Preference> 

To minimize the duplication of code required for two fragments close to it, I created the MyFragmentDelegate class, which supports common fragment methods, and checked an instance of this in MyFragment and MyFragmentHC . Then the method calls in these fragments are simply passed to the delegate:

 class MyFragment { MyFragmentDelegate mDelegate; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return mDelegate.onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) } } class MyFragmentHC { MyFragmentDelegate mDelegate; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return mDelegate.onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) } } 
+2
source

Source: https://habr.com/ru/post/907160/


All Articles