|
10 | 10 |
|
11 | 11 | namespace Microsoft.Maui.Controls.Platform
|
12 | 12 | {
|
13 |
| - internal class CustomFragmentContainer : Fragment |
14 |
| - { |
15 |
| - AView? _pageContainer; |
16 |
| - readonly IMauiContext _mauiContext; |
17 |
| - Action<AView>? _onCreateCallback; |
18 |
| - ViewGroup? _parent; |
19 |
| - AdapterItemKey _adapterItemKey; |
| 13 | + internal class CustomFragmentContainer : Fragment |
| 14 | + { |
| 15 | + AView? _pageContainer; |
| 16 | + readonly IMauiContext _mauiContext; |
| 17 | + Action<AView>? _onCreateCallback; |
| 18 | + ViewGroup? _parent; |
| 19 | + AdapterItemKey _adapterItemKey; |
20 | 20 |
|
21 |
| - public CustomFragmentContainer(AdapterItemKey adapterItemKey, IMauiContext mauiContext) |
22 |
| - { |
23 |
| - _mauiContext = mauiContext; |
24 |
| - _adapterItemKey = adapterItemKey; |
25 |
| - } |
| 21 | + public CustomFragmentContainer(AdapterItemKey adapterItemKey, IMauiContext mauiContext) |
| 22 | + { |
| 23 | + _mauiContext = mauiContext; |
| 24 | + _adapterItemKey = adapterItemKey; |
| 25 | + } |
26 | 26 |
|
27 |
| - public Page Page => _adapterItemKey.Page; |
| 27 | + public Page Page => _adapterItemKey.Page; |
28 | 28 |
|
29 |
| - public static CustomFragmentContainer CreateInstance(AdapterItemKey adapterItemKey, IMauiContext mauiContext) |
30 |
| - { |
31 |
| - return new CustomFragmentContainer(adapterItemKey, mauiContext) { Arguments = new Bundle() }; |
32 |
| - } |
| 29 | + public static CustomFragmentContainer CreateInstance(AdapterItemKey adapterItemKey, IMauiContext mauiContext) |
| 30 | + { |
| 31 | + return new CustomFragmentContainer(adapterItemKey, mauiContext) { Arguments = new Bundle() }; |
| 32 | + } |
33 | 33 |
|
34 |
| - public void SetOnCreateCallback(Action<AView> callback) |
35 |
| - { |
36 |
| - _onCreateCallback = callback; |
37 |
| - } |
| 34 | + public void SetOnCreateCallback(Action<AView> callback) |
| 35 | + { |
| 36 | + _onCreateCallback = callback; |
| 37 | + } |
38 | 38 |
|
39 |
| - public override AView OnCreateView(LayoutInflater inflater, ViewGroup? container, Bundle? savedInstanceState) |
40 |
| - { |
41 |
| - _parent = container ?? _parent; |
| 39 | + public override AView OnCreateView(LayoutInflater inflater, ViewGroup? container, Bundle? savedInstanceState) |
| 40 | + { |
| 41 | + _parent = container ?? _parent; |
42 | 42 |
|
43 |
| - _pageContainer = Page.ToPlatform(_mauiContext, RequireContext(), inflater, ChildFragmentManager); |
44 |
| - _adapterItemKey.SetToStableView(); |
45 |
| - _parent = _parent ?? (_pageContainer.Parent as ViewGroup); |
46 |
| - _onCreateCallback?.Invoke(_pageContainer); |
| 43 | + _pageContainer = Page.ToPlatform(_mauiContext, RequireContext(), inflater, ChildFragmentManager); |
| 44 | + _adapterItemKey.SetToStableView(); |
| 45 | + _parent = _parent ?? (_pageContainer.Parent as ViewGroup); |
| 46 | + _onCreateCallback?.Invoke(_pageContainer); |
47 | 47 |
|
48 |
| - return _pageContainer; |
49 |
| - } |
| 48 | + return _pageContainer; |
| 49 | + } |
50 | 50 |
|
51 |
| - public override void OnDestroy() |
52 |
| - { |
53 |
| - base.OnDestroy(); |
54 |
| - Page?.Handler?.DisconnectHandler(); |
55 |
| - } |
| 51 | + internal static bool InTheThrowsOfTheAdapterHack; |
| 52 | + public override void OnDestroy() |
| 53 | + { |
| 54 | + base.OnDestroy(); |
56 | 55 |
|
57 |
| - public override void OnResume() |
58 |
| - { |
59 |
| - if (_pageContainer == null) |
60 |
| - return; |
| 56 | + if (!InTheThrowsOfTheAdapterHack) |
| 57 | + Page?.Handler?.DisconnectHandler(); |
| 58 | + } |
61 | 59 |
|
62 |
| - _parent = (_pageContainer.Parent as ViewGroup) ?? _parent; |
63 |
| - if (_pageContainer.Parent == null && _parent != null) |
64 |
| - { |
65 |
| - // Re-add the view to the container if Android removed it |
66 |
| - // Because we are re-using views inside OnCreateView Android |
67 |
| - // will remove the "previous" view from the parent but since our |
68 |
| - // "previous" view and "current" view are the same we have to re-add it |
69 |
| - _parent.AddView(_pageContainer); |
70 |
| - } |
| 60 | + public override void OnResume() |
| 61 | + { |
| 62 | + if (_pageContainer == null) |
| 63 | + return; |
71 | 64 |
|
72 |
| - base.OnResume(); |
73 |
| - } |
74 |
| - } |
| 65 | + _parent = (_pageContainer.Parent as ViewGroup) ?? _parent; |
| 66 | + if (_pageContainer.Parent == null && _parent != null) |
| 67 | + { |
| 68 | + // Re-add the view to the container if Android removed it |
| 69 | + // Because we are re-using views inside OnCreateView Android |
| 70 | + // will remove the "previous" view from the parent but since our |
| 71 | + // "previous" view and "current" view are the same we have to re-add it |
| 72 | + _parent.AddView(_pageContainer); |
| 73 | + } |
| 74 | + |
| 75 | + base.OnResume(); |
| 76 | + } |
| 77 | + } |
75 | 78 | }
|
76 | 79 | #endif
|
0 commit comments