如何在打开Fragment时清除返回栈?附相关实现代码
问题:打开Fragment时清除返回栈,避免按返回栈顺序返回
每次打开Fragment时它都会被加入返回栈,导致点击返回按钮会按栈的顺序返回页面。我需要在每次打开Fragment时清除返回栈,请问该如何实现?
我的代码如下:
public class MainActivity extends AppCompatActivity { private FragmentManager fragmentManager; FragmentTransaction transaction; LinearLayout firstColumn,secondColumn,thirdColumn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentManager = getSupportFragmentManager(); firstColumn = (LinearLayout) findViewById(R.id.first_column); secondColumn = (LinearLayout) findViewById(R.id.second_column); firstColumn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container_frame_layout, new FirstFragment()); transaction.commit(); } }); secondColumn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.main_container_frame_layout, new SecondFragment()); transaction.commit(); } }); } }
解决方案
要实现每次打开新Fragment时清空返回栈,你可以利用FragmentManager的返回栈操作方法,在提交新的Fragment事务前彻底清空现有返回栈。具体步骤如下:
1. 清空返回栈的核心操作
在每次创建FragmentTransaction后、提交前,添加一段代码判断返回栈是否有内容,如果有就立即清空:
if (getSupportFragmentManager().getBackStackEntryCount() > 0) { // POP_BACK_STACK_INCLUSIVE参数会把所有栈内Fragment都弹出(包括栈顶) getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); }
这里用popBackStackImmediate()是因为它会立即执行栈的弹出操作,比异步的popBackStack()更可靠,能确保新Fragment提交前栈已经清空。
2. 修改后的完整代码
把这段逻辑加入到每个按钮的点击事件里,修改后的代码如下:
public class MainActivity extends AppCompatActivity { private FragmentManager fragmentManager; FragmentTransaction transaction; LinearLayout firstColumn,secondColumn,thirdColumn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentManager = getSupportFragmentManager(); firstColumn = (LinearLayout) findViewById(R.id.first_column); secondColumn = (LinearLayout) findViewById(R.id.second_column); firstColumn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { transaction = getSupportFragmentManager().beginTransaction(); // 清空返回栈 if (getSupportFragmentManager().getBackStackEntryCount() > 0) { getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); } transaction.replace(R.id.main_container_frame_layout, new FirstFragment()); transaction.commit(); } }); secondColumn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { transaction = getSupportFragmentManager().beginTransaction(); // 清空返回栈 if (getSupportFragmentManager().getBackStackEntryCount() > 0) { getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); } transaction.replace(R.id.main_container_frame_layout, new SecondFragment()); transaction.commit(); } }); } }
额外说明
- 如果你不需要当前Fragment加入返回栈(点击返回键直接退出Activity),就不要调用
transaction.addToBackStack(null);,这样返回栈清空后新Fragment也不会被加入,完美符合你的需求。 - 如果需要当前Fragment留在返回栈里(比如点击返回键回到桌面而不是退出),可以在
transaction.commit()前加上transaction.addToBackStack(null);,但此时返回栈里只会有当前这一个Fragment。
内容的提问来源于stack exchange,提问作者Bassem Jlassi
相关产品推荐
相关产品推荐

