20
20
21
21
import android .os .Bundle ;
22
22
import androidx .fragment .app .Fragment ;
23
+ import androidx .fragment .app .FragmentTransaction ;
23
24
import android .view .LayoutInflater ;
24
25
import android .view .View ;
25
26
import android .view .ViewGroup ;
26
- import androidx .activity .OnBackPressedCallback ;
27
27
import androidx .annotation .LayoutRes ;
28
28
import androidx .annotation .NonNull ;
29
29
import androidx .annotation .Nullable ;
30
+ import androidx .transition .Transition ;
31
+ import androidx .transition .TransitionListenerAdapter ;
30
32
import com .google .android .material .transition .MaterialSharedAxis ;
31
33
import io .material .catalog .feature .DemoFragment ;
32
34
@@ -36,14 +38,6 @@ public class TransitionSharedAxisDemoFragment extends DemoFragment {
36
38
private static final int LAYOUT_RES_ID_START = R .layout .cat_transition_shared_axis_start ;
37
39
private static final int LAYOUT_RES_ID_END = R .layout .cat_transition_shared_axis_end ;
38
40
39
- private final OnBackPressedCallback onBackPressedCallback =
40
- new OnBackPressedCallback (/* enabled= */ false ) {
41
- @ Override
42
- public void handleOnBackPressed () {
43
- replaceFragment (LAYOUT_RES_ID_START );
44
- }
45
- };
46
-
47
41
private SharedAxisHelper sharedAxisHelper ;
48
42
49
43
@ Override
@@ -58,12 +52,18 @@ public View onCreateDemoView(
58
52
@ Override
59
53
public void onViewCreated (@ NonNull View view , @ Nullable Bundle bundle ) {
60
54
sharedAxisHelper = new SharedAxisHelper (view .findViewById (R .id .controls_layout ));
61
- requireActivity ().getOnBackPressedDispatcher ().addCallback (this , onBackPressedCallback );
62
55
63
- replaceFragment (LAYOUT_RES_ID_START );
56
+ Fragment fragment = TransitionSimpleLayoutFragment .newInstance (LAYOUT_RES_ID_START );
57
+
58
+ requireActivity ()
59
+ .getSupportFragmentManager ()
60
+ .beginTransaction ()
61
+ .replace (R .id .fragment_container , fragment )
62
+ .commit ();
64
63
65
64
sharedAxisHelper .setBackButtonOnClickListener (v -> replaceFragment (LAYOUT_RES_ID_START ));
66
65
sharedAxisHelper .setNextButtonOnClickListener (v -> replaceFragment (LAYOUT_RES_ID_END ));
66
+ sharedAxisHelper .updateButtonsEnabled (true );
67
67
}
68
68
69
69
private void replaceFragment (@ LayoutRes int layoutResId ) {
@@ -73,14 +73,28 @@ private void replaceFragment(@LayoutRes int layoutResId) {
73
73
// Set the transition as the Fragment's enter transition. This will be used when the fragment
74
74
// is added to the container and re-used when the fragment is removed from the container.
75
75
fragment .setEnterTransition (createTransition (entering ));
76
+ if (entering ) {
77
+ fragment .setReturnTransition (createTransition (false ));
78
+ } else {
79
+ // Pop the backstack if manually transitioning to the start fragment to remove the end
80
+ // fragment from the back stack without a back event.
81
+ requireActivity ().getSupportFragmentManager ().popBackStack ();
82
+ }
83
+
84
+ getFragmentTransaction (fragment , entering ).commit ();
85
+ }
76
86
77
- getChildFragmentManager ()
78
- .beginTransaction ()
79
- .replace (R .id .fragment_container , fragment )
80
- .commit ();
87
+ private FragmentTransaction getFragmentTransaction (@ NonNull Fragment fragment , boolean entering ) {
88
+ return entering
89
+ ? getFragmentTransaction (fragment ).addToBackStack (/* name= */ null )
90
+ : getFragmentTransaction (fragment );
91
+ }
81
92
82
- sharedAxisHelper .updateButtonsEnabled (!entering );
83
- onBackPressedCallback .setEnabled (entering );
93
+ private FragmentTransaction getFragmentTransaction (@ NonNull Fragment fragment ) {
94
+ return requireActivity ()
95
+ .getSupportFragmentManager ()
96
+ .beginTransaction ()
97
+ .replace (R .id .fragment_container , fragment );
84
98
}
85
99
86
100
private MaterialSharedAxis createTransition (boolean entering ) {
@@ -92,6 +106,18 @@ private MaterialSharedAxis createTransition(boolean entering) {
92
106
// Fragment's layout.
93
107
transition .addTarget (R .id .start_root );
94
108
transition .addTarget (R .id .end_root );
109
+ transition .addListener (
110
+ new TransitionListenerAdapter () {
111
+ @ Override
112
+ public void onTransitionStart (@ NonNull Transition transition ) {
113
+ sharedAxisHelper .updateButtonsEnabled (!entering );
114
+ }
115
+
116
+ @ Override
117
+ public void onTransitionCancel (@ NonNull Transition transition ) {
118
+ sharedAxisHelper .updateButtonsEnabled (entering );
119
+ }
120
+ });
95
121
96
122
return transition ;
97
123
}
0 commit comments