20
20
21
21
import android .os .Bundle ;
22
22
import androidx .fragment .app .Fragment ;
23
+ import androidx .fragment .app .FragmentManager ;
24
+ import androidx .fragment .app .FragmentManager .FragmentLifecycleCallbacks ;
25
+ import androidx .fragment .app .FragmentTransaction ;
23
26
import android .util .SparseIntArray ;
24
27
import android .view .LayoutInflater ;
25
28
import android .view .View ;
29
32
import androidx .annotation .NonNull ;
30
33
import androidx .annotation .Nullable ;
31
34
import com .google .android .material .bottomnavigation .BottomNavigationView ;
35
+ import com .google .android .material .navigation .NavigationBarView ;
32
36
import com .google .android .material .transition .MaterialFadeThrough ;
33
37
import io .material .catalog .feature .DemoFragment ;
34
38
@@ -37,6 +41,12 @@ public class TransitionFadeThroughDemoFragment extends DemoFragment {
37
41
38
42
private static final SparseIntArray LAYOUT_RES_MAP = new SparseIntArray ();
39
43
44
+ private final NavigationBarView .OnItemSelectedListener onItemSelectedListener =
45
+ item -> {
46
+ replaceFragment (item .getItemId (), /* addToBackStack= */ true );
47
+ return true ;
48
+ };
49
+
40
50
static {
41
51
LAYOUT_RES_MAP .append (R .id .action_albums , R .layout .cat_transition_fade_through_albums_fragment );
42
52
LAYOUT_RES_MAP .append (R .id .action_photos , R .layout .cat_transition_fade_through_photos_fragment );
@@ -51,35 +61,73 @@ public View onCreateDemoView(
51
61
52
62
@ Override
53
63
public void onViewCreated (@ NonNull View view , @ Nullable Bundle bundle ) {
64
+ BottomNavigationView bottomNavigationView = view .findViewById (R .id .bottomnavigation );
65
+ bottomNavigationView .setOnItemSelectedListener (onItemSelectedListener );
66
+
67
+ requireActivity ()
68
+ .getSupportFragmentManager ()
69
+ .registerFragmentLifecycleCallbacks (
70
+ new FragmentLifecycleCallbacks () {
71
+ @ Override
72
+ public void onFragmentStarted (
73
+ @ NonNull FragmentManager fragmentManager , @ NonNull Fragment fragment ) {
74
+ super .onFragmentStarted (fragmentManager , fragment );
75
+ Integer itemId = getItemIdFromFragmentTag (fragment .getTag ());
76
+ if (itemId != null && bottomNavigationView .getSelectedItemId () != itemId ) {
77
+ // Workaround to avoid breaking the demo by recreating the fragment on back,
78
+ // since the FragmentManager handles replacing the fragment instead.
79
+ bottomNavigationView .setOnItemSelectedListener (null );
80
+ bottomNavigationView .setSelectedItemId (itemId );
81
+ bottomNavigationView .setOnItemSelectedListener (onItemSelectedListener );
82
+ }
83
+ }
84
+ },
85
+ true );
86
+ replaceFragment (R .id .action_albums , /* addToBackStack= */ false );
87
+ }
54
88
55
- replaceFragment (R .id .action_albums );
89
+ @ Nullable
90
+ private Integer getItemIdFromFragmentTag (@ Nullable String fragmentTag ) {
91
+ try {
92
+ if (fragmentTag != null ) {
93
+ return Integer .parseInt (fragmentTag );
94
+ }
95
+ } catch (NumberFormatException numberFormatException ) {
96
+ // Ignore; we only care about TransitionSimpleLayoutFragments with item id tags.
97
+ }
98
+ return null ;
99
+ }
56
100
57
- BottomNavigationView bottomNavigationView = view .findViewById (R .id .bottomnavigation );
58
- bottomNavigationView .setOnNavigationItemSelectedListener (
59
- item -> {
60
- replaceFragment (item .getItemId ());
61
- return true ;
62
- });
101
+ @ NonNull
102
+ private String convertItemIdToFragmentTag (@ IdRes int itemId ) {
103
+ return String .valueOf (itemId );
63
104
}
64
105
65
106
@ LayoutRes
66
107
private static int getLayoutForItemId (@ IdRes int itemId ) {
67
108
return LAYOUT_RES_MAP .get (itemId );
68
109
}
69
110
70
- private void replaceFragment (@ IdRes int itemId ) {
111
+ private void replaceFragment (@ IdRes int itemId , boolean addToBackStack ) {
71
112
Fragment fragment = TransitionSimpleLayoutFragment .newInstance (getLayoutForItemId (itemId ));
72
113
// Set the transition as the Fragment's enter transition. This will be used when the fragment
73
114
// is added to the container and re-used when the fragment is removed from the container.
74
115
fragment .setEnterTransition (createTransition ());
75
116
76
- getChildFragmentManager ()
77
- .beginTransaction ()
78
- .replace (R .id .fragment_container , fragment )
79
- .commit ();
117
+ FragmentTransaction fragmentTransaction =
118
+ requireActivity ()
119
+ .getSupportFragmentManager ()
120
+ .beginTransaction ()
121
+ .setReorderingAllowed (true )
122
+ .replace (R .id .fragment_container , fragment , convertItemIdToFragmentTag (itemId ));
123
+
124
+ if (addToBackStack ) {
125
+ fragmentTransaction .addToBackStack (convertItemIdToFragmentTag (itemId ));
126
+ }
127
+ fragmentTransaction .commit ();
80
128
}
81
129
82
- private MaterialFadeThrough createTransition (){
130
+ private MaterialFadeThrough createTransition () {
83
131
MaterialFadeThrough fadeThrough = new MaterialFadeThrough ();
84
132
85
133
// Add targets for this transition to explicitly run transitions only on these views. Without
0 commit comments