当前位置: 动力学知识库 > 问答 > 编程问答 >

xamarin - Sync Navigation Drawer menu on Back stack

问题描述:

I followed the https://github.com/jamesmontemagno/Xam.NavDrawer example and Im able to successfully use the drawer layout with fragments(infact nested fragments with view pager). I have one issue, when I click the back button the navigation drawer menu item on the left side is not synced with the fragment that is shown.

This is how I navigate to other fragments

 SupportFragmentManager.BeginTransaction().Replace(Resource.Id.content, fragment).AddToBackStack(fragment.Name).Commit();

I tried the OnAttachFragment method, but its not called on back stack. I also tried the SupportFragmentManager BackStackChanged method, but I could not get the current fragment that is in the view to update the navigation drawer menu title.

网友答案:

I had the same issue and couldn't find any solution as well. Although this question is kinda old, my solution may help someone. I'm not sure if it's the best solution but it works for me.

So first you need to add variable to store ID of previously checked item:

private int previousItemChecked;

set it initially to your default checked item:

if (savedInstanceState == null) {
    selectItem(0);
    previousItemChecked = 0;
}

then edit the fragment transaction so that the transaction title in backstack contains position of the previously checked item converted to string and after the transaction is done set the variable previousItemChecked to the currently checked item id:

fragmentManager.beginTransaction().replace(R.id.content_frame, selectedFragment).addToBackStack(""+previousItemChecked).commit();
previousItemChecked = mDrawerList.getCheckedItemPosition();

and finally in method onBackPressed you need to get the string previously assigned to fragment transaction, parse it to int and update the drawer according to the obtained id:

@Override
public void onBackPressed() {
    if(fragmentManager.getBackStackEntryCount() > 0) {
        String title = fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount()-1).getName();
        int pos = Integer.parseInt(title);
        previousItemChecked = pos;
        mDrawerList.setItemChecked(pos, true);
    }
    super.onBackPressed();
}

I took the code from my app created in Android Studio so it isn't for Xamarin but if you update the code it should work with it too. What's important here is the idea how it's done. I hope the answer is understandable and will help someone.

网友答案:

I had the same issue and I solved like this:

Inside selectItem we are passing the position Item; So if position is 0 (or whatever is fragment we want it appears as first indipendently from it's position on the menu) we have to avoid to save the first transaction. So...

private void selectItem(position){

    //...

    if (position != 0)
    {
        SupportFragmentManager.BeginTransaction()
            .Replace(Resource.Id.content_frame, fragment)
            .AddToBackStack(fragment.Name)
            .Commit();
    }
    else
    {
        SupportFragmentManager.BeginTransaction()
            .Replace(Resource.Id.content_frame, fragment)
            .Commit();
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: