Forum Xamarin.Android

Fragment getting null in host activity.

Hi,
I want to call fragment method from another fragment, so I find relevant fragment in host activity and call method of this fragment. But some time I getting this fragment null.

Suppose I want to save data of fragment A from all other fragment. There is 4 fragment like A, B, C and D. When I click save button from fragment A and B then it working fine but When I save data from fragment C and D then Fragment A getting null.

Here is my code :

HostActivity.cs

public class HostActivityView : MvxCachingFragmentCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

    try
    {               

        SetContentView(Resource.Layout.HostActivityView);

        var toolbar = FindViewById<Toolbar>(Resource.Id.myToolbar);

        if (toolbar != null)
        {
            // Toolbar will now take on default actionbar characteristics
            SetSupportActionBar(toolbar);                    
        }

        // View Pager
        var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
        if (viewPager != null)
        {
            // Add tabs in view pager
            var fragments = new List<MvxFragmentStatePagerAdapter2.FragmentInfo>
        {
            new MvxFragmentStatePagerAdapter2.FragmentInfo("Ticket", typeof(Tab_Ticket), typeof(TicketEditViewModel)),
            new MvxFragmentStatePagerAdapter2.FragmentInfo("Employee", typeof(Tab_Employee), typeof(EmployeeViewModel)),
            new MvxFragmentStatePagerAdapter2.FragmentInfo("Response", typeof(Tab_Correspondence), typeof(ResponseViewModel)),
            new MvxFragmentStatePagerAdapter2.FragmentInfo("Expense", typeof(Tab_Expenses), typeof(ExpenseViewModel)),                   

        };
            viewPager.Adapter = new MvxFragmentStatePagerAdapter2(this, SupportFragmentManager, fragments);
        }

        var tabLayout = FindViewById<TabLayout>(Resource.Id.tabs);
        tabLayout.SetupWithViewPager(viewPager);              

    }
    catch (Exception ex)
    {
        Mvx.Resolve<IUserInteraction>().Alert(ex.Message);
    }

}

public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.menu_accept:

   Tab_Ticket tabTicket = (Tab_Ticket)SupportFragmentManager.FindFragmentByTag("Tab_Ticket");
   if (tabTicket != null)
   {
        tabTicket.OnOptionsItemSelected(item);
        handled = true;
   }                   

   break;       

}
}
}

Here is my fragment Code : Fragment.cs :

public class Tab_Ticket : MvxFragment
{
public override View OnCreateView(Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);

    var view = this.BindingInflate(Resource.Layout.frg_tab_ticket, null);
    return view;
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
    switch (item.ItemId)
    {

        case Resource.Id.menu_accept:
            this.ViewModel.CmdTicketSave.Execute(null);
            return true;
            break;
    }
}

}

Please suggest me where I going wrong?

Thank you

Sign In or Register to comment.