-using System;
using Android.App;
using Android.Content;
-using Android.OS;
using Fragment = Android.Support.V4.App.Fragment;
using FragmentManager = Android.Support.V4.App.FragmentManager;
using FragmentTransaction = Android.Support.V4.App.FragmentTransaction;
namespace Xamarin.Forms.Platform.Android.AppCompat
{
- internal class MasterDetailContainer : Xamarin.Forms.Platform.Android.MasterDetailContainer, IManageFragments
+ internal class MasterDetailContainer : Android.MasterDetailContainer, IManageFragments
{
PageContainer _pageContainer;
FragmentManager _fragmentManager;
MasterDetailPage _parent;
Fragment _currentFragment;
bool _disposed;
+ FragmentTransaction _transaction;
public MasterDetailContainer(MasterDetailPage parent, bool isMaster, Context context) : base(parent, isMaster, context)
{
transaction.RemoveEx(_currentFragment);
transaction.SetTransitionEx((int)FragmentTransit.None);
- // This is a removal of a fragment that's not going on the back stack; there's no reason to care
- // whether its state gets successfully saved, since we'll never restore it. Ergo, CommitAllowingStateLoss
- transaction.CommitAllowingStateLossEx();
+ if (IsAttachedToWindow)
+ ExecuteTransaction(transaction);
+ else
+ _transaction = transaction;
_currentFragment = null;
}
transaction.AddEx(Id, fragment);
transaction.SetTransitionEx((int)FragmentTransit.None);
- // We don't currently support fragment restoration
- // So we don't need to worry about loss of this fragment's state
- transaction.CommitAllowingStateLossEx();
+ if (IsAttachedToWindow)
+ ExecuteTransaction(transaction);
+ else
+ _transaction = transaction;
_currentFragment = fragment;
}
}
+ protected override void OnAttachedToWindow()
+ {
+ base.OnAttachedToWindow();
+
+ if (_transaction == null)
+ return;
+
+ ExecuteTransaction(_transaction);
+
+ _transaction = null;
+ }
+
protected override void Dispose(bool disposing)
{
if (_disposed)
if (_fragmentManager == null)
_fragmentManager = fragmentManager;
}
+
+ void ExecuteTransaction(FragmentTransaction transaction)
+ {
+ // We don't currently support fragment restoration
+ // So we don't need to worry about loss of this fragment's state
+ transaction.CommitAllowingStateLossEx();
+
+ // The transaction need to be executed after View has been attached
+ // So Fragment Manager can find the View being added
+ FragmentManager.ExecutePendingTransactionsEx();
+ }
}
}
\ No newline at end of file
-using FragmentTransaction = Android.Support.V4.App.FragmentTransaction;
+using FragmentTransaction = Android.Support.V4.App.FragmentTransaction;
using Fragment = Android.Support.V4.App.Fragment;
using FragmentManager = Android.Support.V4.App.FragmentManager;
return fragmentTransaction.CommitAllowingStateLoss();
}
- public static bool ExecutePendingTransactionsEx(this FragmentManager fragmentTransaction)
+ public static bool ExecutePendingTransactionsEx(this FragmentManager fragmentManager)
{
- return fragmentTransaction.ExecutePendingTransactions();
+ return fragmentManager.ExecutePendingTransactions();
}
- public static FragmentTransaction BeginTransactionEx(this FragmentManager fragmentTransaction)
+ public static FragmentTransaction BeginTransactionEx(this FragmentManager fragmentManager)
{
- return fragmentTransaction.BeginTransaction();
+ return fragmentManager.BeginTransaction();
}
}
}
\ No newline at end of file