Fragment越来越多的被开发者使用,估计即将成为一种趋势。最近被很多人问关于fragment的跳转问题,再加上本人也研究了很多fragment的用法,看了关于fragment的大部分API,特在此说下本人的菜鸟看法
实现一:简单的从一个fragment中跳转到另一个fragment,按返回键返回上一个fragment
1、实现效果图
项目结构图
MainActivity.java
package com.xwj.fragmenttest1; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; public class MainActivity extends FragmentActivity { private FragmentManager fragmentManager; private FragmentTransaction fragmentTransaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentManager = getSupportFragmentManager(); fragmentTransaction = fragmentManager.beginTransaction(); FragmentTest1 fragmentTest1 = new FragmentTest1(); fragmentTransaction.replace(R.id.fragment_main, fragmentTest1, "fragmentTest1"); fragmentTransaction.commit(); } @Override public void onBackPressed() { if (fragmentManager.findFragmentByTag("fragmentTest1") != null && fragmentManager.findFragmentByTag("fragmentTest1") .isVisible()) { System.out.println("12"); MainActivity.this.finish(); } else { super.onBackPressed(); } } }
注:FragmentActivity的FragmentManager是处理Fragment Transaction的而不是处理Fragment
FragmentTransaction默认并不会主动被加入到BackStack中,需要调用addToBackStack(String tag)方法。参数'fragmentTest1'作为本次加入BackStack的Transaction的标志
和addToBackStack相对应的接口方法是popBackStack()和popBackStackImmediate()
FragmentTest1.java
package com.xwj.fragmenttest1; import android.app.Activity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; public class FragmentTest1 extends android.support.v4.app.Fragment { private static final String TAG = "FragmentTest1---------"; @Override public void onCreate(Bundle savedInstanceState) { System.out.println(TAG + "onCreate"); super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { System.out.println(TAG + "onCreateView"); View view = inflater.inflate(R.layout.test1_fragment, null); Button jumpBtn = (Button) view.findViewById(R.id.btn_jump); jumpBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { getActivity() .getSupportFragmentManager() .beginTransaction() .replace(R.id.fragment_main, new Fragmenttest2(), "Fragmenttest2") .addToBackStack("Fragmenttest2").commit(); } }); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { System.out.println(TAG + "onActivityCreated"); super.onActivityCreated(savedInstanceState); } @Override public void onAttach(Activity activity) { System.out.println(TAG + "onAttach"); super.onAttach(activity); } @Override public void onDestroy() { System.out.println(TAG + "onDestroy"); super.onDestroy(); } @Override public void onDestroyView() { System.out.println(TAG + "onDestroyView"); super.onDestroyView(); } @Override public void onPause() { System.out.println(TAG + "onPause"); super.onPause(); } @Override public void onResume() { System.out.println(TAG + "onResume"); super.onResume(); } @Override public void onStart() { System.out.println(TAG + "onStart"); super.onStart(); } @Override public void onStop() { System.out.println(TAG + "onStop"); super.onStop(); } @Override public void onDetach() { System.out.println(TAG + "onDetach"); super.onDetach(); } }
.addToBackStack("Fragmenttest2")记得一定要添加,这是进栈,方便你从Fragmenttest2中返回时不至于退出程序
至于Fragmenttest2只是简单的界面,在此就不贴出来了
源码下载:点击
下面举例说明Transaction BackStack的进栈和出栈
如下图,使用FragmentActivity的FragmentManager创建Trasaction1并提交,使页面显示AFragment。这时调用popBackStack()就会移除AFragment并返回FragmentActivity。
在AFragment已经在栈顶的情况下,我们再创建Transation2并提交,Transaction 2添加了两个Fragment,提交后页面显示的是CFragment,这时调用popBackStack()就会将Transaction2从BackStack移除,即将CFragment和BFragment同时移除,页面将显示AFragment。
参考资料: