44import android .os .Bundle ;
55import android .support .annotation .Nullable ;
66import android .support .v4 .app .Fragment ;
7+ import android .support .v4 .view .ViewCompat ;
78import android .view .LayoutInflater ;
89import android .view .View ;
910import android .view .ViewGroup ;
11+ import android .widget .Button ;
1012import android .widget .TextView ;
1113
1214import com .blankj .androidutilcode .R ;
15+ import com .blankj .androidutilcode .activity .FragmentActivity ;
1316import com .blankj .utilcode .utils .FragmentUtils ;
1417import com .blankj .utilcode .utils .ToastUtils ;
1518
1619import java .util .Random ;
17- import java .util .Stack ;
1820
1921/**
2022 * <pre>
2527 * </pre>
2628 */
2729public class Demo0Fragment extends Fragment
28- implements View .OnClickListener {
30+ implements View .OnClickListener , FragmentUtils . OnBackClickListener {
2931
3032 private Fragment fragment1 ;
3133
@@ -38,10 +40,9 @@ public static Demo0Fragment newInstance() {
3840 return fragment ;
3941 }
4042
43+ private Button btnShowAboutFragment ;
4144 private TextView tvAboutFragment ;
4245
43- Stack <Fragment > stack = new Stack <>();
44-
4546 @ Override
4647 public View onCreateView (LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
4748 return inflater .inflate (R .layout .fragment_demo0 , container , false );
@@ -50,8 +51,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
5051 @ Override
5152 public void onViewCreated (View view , @ Nullable Bundle savedInstanceState ) {
5253 super .onViewCreated (view , savedInstanceState );
53- view .findViewById (R .id .btn_show_stack ).setOnClickListener (this );
54- view .findViewById (R .id .btn_add ).setOnClickListener (this );
54+ btnShowAboutFragment = (Button ) view .findViewById (R .id .btn_show_about_fragment );
55+ btnShowAboutFragment .setOnClickListener (this );
56+ view .findViewById (R .id .btn_add_hide ).setOnClickListener (this );
57+ view .findViewById (R .id .btn_add_show ).setOnClickListener (this );
5558 view .findViewById (R .id .btn_add_child ).setOnClickListener (this );
5659 view .findViewById (R .id .btn_pop_to_root ).setOnClickListener (this );
5760 view .findViewById (R .id .btn_pop_add ).setOnClickListener (this );
@@ -71,35 +74,50 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
7174 public void onClick (View view ) {
7275 tvAboutFragment .setText ("" );
7376 switch (view .getId ()) {
74- case R .id .btn_show_stack :
75- tvAboutFragment .setText (FragmentUtils .getAllFragments (getFragmentManager ()).toString ());
77+ case R .id .btn_show_about_fragment :
78+ tvAboutFragment .setText ("---all fragments---\n "
79+ + FragmentUtils .getAllFragments (getFragmentManager ()).toString ()
80+ + "\n -------------------\n \n "
81+ + "---stack top---\n "
82+ + FragmentUtils .getAllFragmentsInStack (getFragmentManager ()).toString ()
83+ + "\n ---stack bottom---\n \n "
84+ + "\n topFragment: " + FragmentUtils .getTopFragment (getFragmentManager ()).getClass ().getSimpleName ()
85+ + "\n topShowFragment: " + FragmentUtils .getTopShowFragment (getFragmentManager ()).getClass ().getSimpleName ()
86+ );
87+ break ;
88+ case R .id .btn_add_hide :
89+ FragmentUtils .addFragment (getFragmentManager (), Demo1Fragment .newInstance (), R .id .fragment_container , true , true );
7690 break ;
77- case R .id .btn_add :
78- stack . add ( FragmentUtils .addFragment (getFragmentManager (), Demo1Fragment .newInstance (), R .id .fragment_container , true , true ) );
91+ case R .id .btn_add_show :
92+ FragmentUtils .addFragment (getFragmentManager (), Demo1Fragment .newInstance (), R .id .fragment_container , false , true );
7993 break ;
8094 case R .id .btn_add_child :
8195 FragmentUtils .addFragment (getChildFragmentManager (), Demo2Fragment .newInstance (), R .id .child_fragment_container , false , true );
8296 break ;
8397 case R .id .btn_pop_to_root :
8498 FragmentUtils .popToFragment (getFragmentManager (), Demo1Fragment .class , true );
85- stack .clear ();
8699 break ;
87100 case R .id .btn_pop_add :
88- if (!stack .isEmpty ()) {
89- stack .pop ();
90- }
91- FragmentUtils .popAddFragment (getFragmentManager (), R .id .fragment_container , Demo2Fragment .newInstance (), true );
101+ ViewCompat .setTransitionName (btnShowAboutFragment , "addSharedElement" );
102+ FragmentUtils .popAddFragment (getFragmentManager (), R .id .fragment_container , Demo2Fragment .newInstance (), true , new FragmentUtils .SharedElement (this .btnShowAboutFragment , "btnShowAboutFragment" ));
92103 break ;
93104 case R .id .btn_hide_show :
94- if (!stack .isEmpty ()) {
95- FragmentUtils .hideShowFragment (this , stack .peek ());
105+ Fragment fragment1 = FragmentUtils .findFragment (getFragmentManager (), Demo1Fragment .class );
106+ if (fragment1 != null ) {
107+ FragmentUtils .hideShowFragment (this , fragment1 );
96108 } else {
97- ToastUtils .showLongToast ("please add first!" );
109+ ToastUtils .showLongToast ("please add demo1 first!" );
98110 }
99111 break ;
100112 case R .id .btn_replace :
101- FragmentUtils .replaceFragment (this , newInstance (), false );
113+ (( FragmentActivity ) getActivity ()). rootFragment = FragmentUtils .replaceFragment (this , Demo3Fragment . newInstance (), false );
102114 break ;
103115 }
104116 }
117+
118+ @ Override
119+ public boolean onBackClick () {
120+ FragmentUtils .popToFragment (getFragmentManager (), Demo1Fragment .class , true );
121+ return false ;
122+ }
105123}
0 commit comments