Hi guys I have a little problem. I am using the support library and the navigation drawer, and for each item in the menu I have a separate fragment instead of activity. And a fragment can call a DialogFragment, which works fine, except if I for example open all 4 of my fragments before using the DialogFragment, I have to click the X button which closes the DF (with this.Dismiss
) associated with it, which I found out is the relation to the problem. The more times I open a Fragment, this number of times I have to click the X button to close the DF.
Im using following code to display the fragments:
private void ShowFragment(SupportFragment fragment) { if (fragment.IsVisible) { return; } var trans = SupportFragmentManager.BeginTransaction(); trans.Replace(Resource.Id.LayoutFragmentHolder, fragment); trans.AddToBackStack(null); trans.Commit(); mCurrentFragment = fragment; }
I believe if you put a breakpoint in AddManually
you will see that it is called once for every time you've called NavigationItemSelected
.
You should not be adding an event handler inside of NavigationItemSelected
because it will add the event handler every time that NavigationItemSelected
is called. (See the code +=
adding an additional handler every time).
Moving it outside that scope should resolve your issue.
private void SetUpDrawerContent(NavigationView navigationView) { fragmentDiet.OnDietAddManuallyClickComplete += delegate { AddManually(); }; fragmentDiet.OnDietScanClickComplete += delegate { Scan(); }; navigationView.NavigationItemSelected += (object sender, NavigationView.NavigationItemSelectedEventArgs e) => { e.MenuItem.SetChecked(true); string itemTitle = e.MenuItem.TitleFormatted.ToString(); switch (itemTitle) { case "Diet": ShowFragment(fragmentDiet); break; // ...
Answers
I add a fragment with a tag (
trans.Add(fragment, tag);
) but before doing so I check to see if the fragment exists and remove it first.You may also want to post your code that is showing the
DialogFragment
, because it sounds to me like the code that is showing your dialog fragment is called for every instance of fragment that you have, so a more complete code sample might help narrow down the problem.Thanks, Ill try doing so. Maybe the problem is with the DF...
@MikolajMarciniak.2972 please also post the code where
DialogScanCreate
is calledAnd
AddManually()
is activated here:I believe if you put a breakpoint in
AddManually
you will see that it is called once for every time you've calledNavigationItemSelected
.You should not be adding an event handler inside of
NavigationItemSelected
because it will add the event handler every time thatNavigationItemSelected
is called. (See the code+=
adding an additional handler every time).Moving it outside that scope should resolve your issue.
Yes, this indeed did resolve the issue. Thank you very much mate!