Search code examples
androidxamarinxamarin.formsprismmvvm-light

What is the Prism version of GalaSoft.MvvmLight's AppCompatActivityBase?


I am rewriting an app that supports GalaSoft.MVVMLight in Xamarin.Android to Prism.

This Android project have this particular class GalaSoft.MVVMLight AppCompatActivityBase which inherits from AppCompatActivity.

public class AppCompatActivityBase : AppCompatActivity
    {
        public AppCompatActivityBase()
        {
        }

        internal string ActivityKey
        {
            get;
            private set;
        }

        /// <summary>
        /// The activity that is currently in the foreground.
        /// </summary>
        public static AppCompatActivityBase CurrentActivity
        {
            get;
            private set;
        }

        internal static string NextPageKey
        {
            get;
            set;
        }

        /// <summary>
        /// If possible, discards the current page and displays the previous page
        /// on the navigation stack.
        /// </summary>
        public static void GoBack()
        {
            if (CurrentActivity != null)
            {
                CurrentActivity.OnBackPressed();
            }
        }

        /// <summary>
        /// Overrides <see cref="M:Android.App.Activity.OnResume" />. If you override
        /// this method in your own Activities, make sure to call
        /// base.OnResume to allow the <see cref="T:GalaSoft.MvvmLight.Views.NavigationService" />
        /// to work properly.
        /// </summary>
        protected override void OnResume()
        {
            CurrentActivity = this;
            if (string.IsNullOrEmpty(ActivityKey))
            {
                ActivityKey = NextPageKey;
                NextPageKey = null;
            }
            base.OnResume();
        }

        /// <summary>
        /// Overrides <see cref="M:Android.App.Activity.OnCreate" />. If you override
        /// this method in your own Activities, make sure to call
        /// base.OnCreate to allow the <see cref="T:GalaSoft.MvvmLight.Views.NavigationService" />
        /// to work properly.
        /// </summary>
        protected override void OnCreate(Bundle savedInstanceState)
        {
            // Set CurrentActivity to the first activity that is created
            if (CurrentActivity == null)
            {
                CurrentActivity = this;
                if (string.IsNullOrEmpty(ActivityKey))
                {
                    ActivityKey = NextPageKey;
                    NextPageKey = null;
                }
            }
            base.OnCreate(savedInstanceState);
        }
    }

This AppCompatActivityBase is then referred to in a user preference class as shown below.

   class UserPreferences : IUserPreferences
    {
        const int MaximumHistoryEntries = 5;
        readonly ISharedPreferences preferences;
        readonly ISharedPreferencesEditor editor;

        public UserPreferences()
        {
            preferences = PreferenceManager.GetDefaultSharedPreferences(AppCompatActivityBase.CurrentActivity.ApplicationContext);
            editor = preferences.Edit();
        }

        public event EventHandler Saved;

        public string AssetsExtractedVersion
        {
            get
            {
                return preferences.GetString(AppCompatActivityBase.CurrentActivity.GetString(Resource.String.PrefKeyAssetsExtractedVersion), null);
            }
            set
            {
                editor.PutString(AppCompatActivityBase.CurrentActivity.GetString(Resource.String.PrefKeyAssetsExtractedVersion), value);
            }
        }

        public void Save()
        {
            editor.Commit();
            Saved?.Invoke(this, EventArgs.Empty);
        }

    }

Since my project now supports Prism instead of GalaSoft.MVVMLight. How can I replace AppCompatActivityBase in Prism? My background is in iOS so I'm having trouble understanding the Android end of Xamarin/Prism.


Solution

  • There isn't one. While Prism does have an Android specific binary to support Dependency Injection with the Xamarin.Forms Dependency Resolver which may include a Android.Content.Context, Prism itself is entirely dependent on Xamarin.Forms and runs the exact same across all platforms. As a result there is no need for a Prism specific base Activity