片段上的后退按钮不工作

时间:2015-08-19 11:13:51

标签: android

我有一个片段活动,我可以从哪里开始包含viewpager的片段。在我的片段活动中,我添加了这段代码。

fragment = new ItemPagerFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_frame, fragment);
transaction.addToBackStack(null);
transaction.commit();

现在,当我按下后退按钮时,只出现空白屏幕并且它不会引导我进行片段活动。 我可能做错了什么?

5 个答案:

答案 0 :(得分:1)

试试这种方式,

                   // Create new fragment and transaction
                    Fragment newFragment = new ItemPagerFragment();
                    // consider using Java coding conventions (upper first char class names!!!)
                    FragmentTransaction transaction = getFragmentManager().beginTransaction();
                    // Replace whatever is in the fragment_container view with this fragment,
                    // and add the transaction to the back stack
                    transaction.replace(R.id.content_frame, newFragment);
                    transaction.addToBackStack(null);
                    // Commit the transaction
                    transaction.commit();

修改您的代码

    @Override
public void onBackPressed() {
    if(some condition) {
        // do something
    } else {
        super.onBackPressed();
    }
}

请点击此链接 Handling back button press Inside Fragments

答案 1 :(得分:1)

在包含片段的活动中使用此选项。

@Override
public void onBackPressed() {
    if (fragmentManager.getBackStackEntryCount() > 1) {
        fragmentManager.popBackStack();
    } else
        finish();
}

答案 2 :(得分:0)

您必须告诉片段管理器已按下后退按钮。覆盖onBackPressed

上的Activity
@Override
public void onBackPressed() {
   if (getFragmentManager().getBackStackEntryCount() == 0) {
      super.onBackPressed();      
   } else {
      getFragmentManager().popBackStack();
   }
}

答案 3 :(得分:0)

private void changeFragment (Fragment fragment){


  String backStackName = fragment.getClass().getName();

  FragmentManager manager = getSupportFragmentManager();
  boolean fragmentPopped = manager.popBackStackImmediate (backStackName, 0);

  if (!fragmentPopped){ //fragment not in back stack, create it.
    FragmentTransaction ft = manager.beginTransaction();
    ft.replace(R.id.content_frame, fragment);
    ft.addToBackStack(backStackName );
    ft.commit();
  }
}

答案 4 :(得分:0)

启动片段时为每个片段分配一个TAG。当您想要使用指定的TAG名称检查是否存在片段时,请执行相应的操作。例如:

private final static String TAG_FRAGMENT = "TAG_FRAGMENT";
//when want to add fragment to view
private void showFragment() {
    final Myfragment fragment = new MyFragment();
    final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT);
    transaction.addToBackStack(null);
    transaction.commit();
}
//in activity back pressed check the existence of fragment by TAG
@Override
public void onBackPressed() {
    final Myfragment fragment = (Myfragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);

    if (fragment.allowBackPressed()) { // and then you define a method allowBackPressed with the logic to allow back pressed or not
        super.onBackPressed();
    }
}