Fragment OnAttach() comaptibility

Michael_ShapiroMichael_Shapiro USMember ✭✭✭
edited January 2018 in Xamarin.Android

Can anyone help me with comparability issue, please? A Fragment class has OnAttach(Activity activity) which was deprecated as of api 23. The current signature is OnAttach(Context context). I would like to have a code that can work on api 19 and up.

So, currently I use both methods in my code:

            [Obsolete()]
            public override void OnAttach(Activity activity)
            {
                base.OnAttach(activity);
                if (Convert.ToInt16(Build.VERSION.SdkInt) < 23)
                {
                    DoSomething(activity);
                }
            }
            public override void OnAttach(Context context)
            {
                base.OnAttach(context);
                DoSomething(context);
            }

and it works fine if compiling against api 23 and up. But I also need this code to work and not break on KitKat for instance. But if I try to compile against api 19 then the compiler obviously does not know what OnAttach(Context context) is and gives me an error.

What can I do?

P.S. I do not use, nor I want to use Compact framework.

Tagged:

Best Answers

Answers

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭

    Hi,
    Maybe this Fragment do not compatible with lower level Api.

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭

    @Ahsan_Siddique Fragments are compatible with all APIs... There must be a way to make the code with fragments work in all the versions. Can anyone assist, please?

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭
    edited January 2018

    @JonDouglas - yes, I see the code and it makes perfect sense. But perhaps I am missing something here: when the app is installed on an older phone with the api version, let's say 19... how will it know about the newer signature of the OnAttach() ??? Very sorry if this is a stupid question, but I am genuinely confused here. Also, when I compile and purposely set the targeted framework to KitKat(api19), just to test it locally... it simply throws compile error complaining about not being able to find an override for OnAttach(Context context)....

    Hope you can clarify my confusion and there is an easy explanation.

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭

    oh... I see.. So, by trying to test locally and compiling "backwards" I was basically shooting myself in foot. Self inflicted problem:) I will follow your advice and get rid of the older method in favor of the current one. Hope it all works once I deploy on older phones. Thank you very much!!!

  • JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai

    The other problem is that you don't want to use the Android Support Library it sounds which provides recent platform API features and functionality backwards to old Android versions. Really in this modern day, the Android Support Library is something you can't really "avoid" as it's used in majority of apps for backwards compatibility. You would find android.support.v4.app.Fragment to be much more helpful in these cases.

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭

    @JonDouglas - as for the Support Lib... I guess I never liked "extra" code that can be avoided. Don't want to have an extra dependency and prefer a "pure" approach:) But I might be forced to use it after all - there are more things that got broken once I tried to make my app to be backwards compatible all the way to KitKat :( looks like the Toolbar class is nowhere to be found in KatKat, and I need to use the Activitybar or something like that right? What is the good place to find all these differences btwn apis in an easily searchable format?

  • JonDouglasJonDouglas USXamarin Team, University, Developer Group Leader Xamurai
    edited January 2018

    I personally set my apps to minSdkVersion = 21 as KitKat is going away very rapidly:

    https://developer.android.com/about/dashboards/index.html

    As for support libraries, they are the "pure" approach for supporting old API levels that are for some reason not included in the main platform framework for some reason. Android is fickle in that sense.

    If you tried to not use the support libraries and wanted a "Material Design" looking application, you would not be able to achieve that without writing your own version of a support library on devices < API 21. I would personally advise to "not fight the framework" and use support libraries everywhere in your projects. Some of the APIs in the support libraries are much more flushed out than plain-jane ones as they have to support more APIs.

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭
    edited February 2018

    @JonDouglas - as I started transitioning the app to use the support libraries and learning all different (and many) versions of these libraries, I read here https://developer.xamarin.com/guides/android/platform_features/fragments/providing-backwards-compatibility/ that the Activity that hosts the fragment must inherit from this support library instead of Android.App.Activity. I am at a pickle here - My activities already inherit from a different third party package, namely Galasoft.MVVMLight. Yes, I do extensively use mvvm pattern through the app.

    So, here is the dilemma - I can inherit ONLY from one base in .NET. Does it mean I am screwed? Perhaps I am misunderstanding how to use the support library?

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭

    ok.. so the hierarchy is - I inherit from Jim's base, and Jim inherited from MVMLight's base... That's a lot of inheritance:) But I guess I have no choice at his point.

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭

    hey, it's a physiological barrier:) do you know how long it took me to trust Laurent's code before I included it in my app? Now I have to learn to trust Jim. Yes,. I am bit (a lot?) paranoid :)

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭
    edited February 2018

    @JonDouglas - Please, a bit of clarification about support libraries. Jim's implementation derives from Android.Support.V7.AppCompact and addresses both - ActionBar compatibility and mvvmlight usage in one project.
    To make Fragments backwards compatible, the activity that calls the fragment must inherit from Android.Support.V4.FragmentActivity instead of Android.App.Activity (according to this https://developer.xamarin.com/guides/android/platform_features/fragments/providing-backwards-compatibility/ )

    Does it mean I need to write an implementation similar to Jim's but for FragmentActivity class? And if so, it looks like I am stuck again deciding which compatibility to choose for a single activity - Fragments or ActivityBar? Am i missing something here? Does deriving from V7.AppCompact handles all of Fragments stuff?

    Thank you so much for help,
    mike

  • Michael_ShapiroMichael_Shapiro USMember ✭✭✭

    oh... v7.app.AppCompactActivity derives from v4.app.FragmentActivity. I guess I answered my own (stupid) question :)

Sign In or Register to comment.