首页 > 解决方案 > 使用片段和活动android的回栈和回压

问题描述

我在一个活动中使用了 3 个片段,使用来自第 3 个片段的帧布局,它应该转到一个活动,然后在该活动的后按时,它应该重定向到第 3 个片段,并且从第 3 个片段的后按它应该重定向到第一个片段而没有空白屏幕?.我有黑屏和循环

标签: androidandroid-activityfragment

解决方案


覆盖 onBackPressed() 并在内部处理替换。

@Override
public void onBackPressed() {

  //Check current fragment 
  Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
  if(f instanceof FragmentThird) {

  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

  // Replace whatever is in the fragment_container view with this fragment,
  // and add the transaction to the back stack if needed
  transaction.replace(R.id.fragment_container, fragmentFirst);
  transaction.addToBackStack(null);

  // Commit the transaction
  transaction.commit();
  }
  return;

}


推荐阅读