Base Activity For All Activity

I need to store some sensitive data such as JOSN TOKEN into Local Storage. My App will have Multiple Activities, I would like to check the Token Validity on every single activity, I dont want to copy/paste the same logic on all Activities. is there anyway I can have something like a base activity which has the logic and all other activities inherit from it?

Currently all my activities look like this...

    public class MainActivity : AppCompatActivity
    {
    }
    public class RegisterActivity : AppCompatActivity
    {
    }
    public class ContentActivity : AppCompatActivity
    {
    }

I am just looking for a easy to write logic once and have all activities re-use it.


Yes, you can create a base class which inherits from class AppCompatActivity.

[Activity(Label = "BaseActivity")]
public class BaseActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your application here

        Preferences.Set("my_key", "123456");
    }

    protected override void OnResume()
    {
        base.OnResume();

        var myValue = Preferences.Get("my_key", "default_value");

        System.Diagnostics.Debug.WriteLine("-------------> value is:  "+myValue);
    }
}

And make other activities extend from the BaseActivity ,for example:

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : BaseActivity
{

    Button button;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

    }
}

update

so if I have a Menu, which I need to share on all activities, is there any concept for base layout as well?

If you want to add menu for your all activities, you can just add your menus in BaseActivity , please refer to the following code:

1.create your menu (mymenu.xml)in folder menu in your Resources folder

<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/item1"
        android:title="Item 1"/>
  <item android:id="@+id/item2"
        android:title="Item 2"/>
  <item android:id="@+id/item3"
        android:title="Item 3"/>
</menu>

2.modify BaseActivity as follows:

   public class BaseActivity : AppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your application here

            Preferences.Set("my_key", "123456");
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {

            MenuInflater.Inflate(Resource.Menu.mymenu, menu);

            return true;
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Resource.Id.item1:
                    //Do stuff for item1

                    System.Diagnostics.Debug.WriteLine("--------> select item1");
                    return true;
                case Resource.Id.item2:
                    //Do stuff for item2
                    System.Diagnostics.Debug.WriteLine("--------> select item2");
                    return true;
                case Resource.Id.item3:
                    //Do stuff for item3

                    System.Diagnostics.Debug.WriteLine("--------> select item3");
                    return true;
                default:
                    return false;
            }
        }
    }