Search code examples
androidxamarinnavigation-drawerandroid-actionbar-compat

Xamarin.Droid - DrawerLayout.SetDrawerLockMode(x) crashes V7.SupportActionBar


I'm trying to Disable the NavDrawer while I'm in an Android Fragment. The following code is currently working:

public class MainActivity : BaseActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
         SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);
    }

    public void LockNavigationDrawer()
    {
        if(drawerLayout != null)
            drawerLayout.SetDrawerLockMode(DrawerLayout.LockModeLockedClosed);
    }

    public void UnlockNavigationDrawer()
    {
        if (drawerLayout != null)
            drawerLayout.SetDrawerLockMode(DrawerLayout.LockModeUnlocked);
    }
}

Then I call the method in the fragment:

((MainActivity)Activity).LockNavigationDrawer();

This is causing my ((Android.Support.V7.App.AppCompatActivity)Activity).SupportActionBar a Null value at the MainActivity when we start that activity.

If i don't use the method LockNavigationDrawer the line below works as expected. When i call the method. It causes a NullPointerException because the SupportActionBar is always (null).

SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);


Solution

  • The main reason it is my Parent class BaseActivity was override the OnCreate method async:

    protected async override void OnCreate(Bundle bundle)
    {
    }
    

    So the Child class sometimes trigger the LoadCompleted before the Parent, and the SupportActionBar never was instantiated.