将特定片段置于其他片段的顶部

时间:2018-02-01 09:08:16

标签: android android-fragments

如何将fragment置于其他fragments之上?

我添加fragment如下:

@OnClick(R.id.imbEvents)
void clickEvents() {
    String tagName = returnStatusFragment(getString(R.string.events_fragment));
    if (!TextUtils.isEmpty(tagName) && tagName.equals(getString(R.string.events_fragment))) {
        //Fragment is opened
    } else {
        if (getActivity() != null) {
            frameWorkTableFragment.setVisibility(View.VISIBLE);
            EventsFragment eventsFragment = new EventsFragment();
            getActivity().getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frame_work_table_fragment,
                            eventsFragment,
                            getString(R.string.events_fragment))
                    .addToBackStack(getString(R.string.events_fragment))
                    .commit();
        }
    }
}

我使用下面的代码将fragment置于顶部,但最后关闭fragments

getActivity().getSupportFragmentManager().popBackStack(tagName, 0);

returnStatusFragment的方法是:

private String returnStatusFragment(String tagName) {
    android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    Fragment tempFragment = fragmentManager.findFragmentByTag(tagName);
    if (tempFragment != null) {
        return tempFragment.getTag();
    } else {
        return "";
    }
}

我的doBackTabZero

@Override
public void doBackTabZero(String nameFragment) {
    android.support.v4.app.FragmentManager fragmentManagers = getActivity().getSupportFragmentManager();
    Log.i("ASDSADASDSASD", "3 " + nameFragment + " " + fragmentManagers.getBackStackEntryCount());
    if (fragmentManagers.getBackStackEntryCount() > 1) {
        fragmentManagers.popBackStackImmediate();
    } else {
        if (nameFragment.equals("EventsFragment")) {
            BusDisplayStatusFrameLayout busDisplayStatusFrameLayout = new BusDisplayStatusFrameLayout();
            busDisplayStatusFrameLayout.setDisplayStatusFrameLayout(1);
            EventBus.getDefault().post(busDisplayStatusFrameLayout);
            android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentManager.popBackStackImmediate();
            fragmentTransaction.remove(EventsFragment.this);
            fragmentTransaction.commit();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

替换此

getActivity().getSupportFragmentManager().popBackStack(tagName, 0);

Fragment eventsFragment = fragmentManager.findFragmentByTag(getString(R.string.events_fragment));
getActivity().getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frame_work_table_fragment,
                            eventsFragment,
                            getString(R.string.events_fragment)).commit();//don't add to back stack again

onBackPressed

 @Override 
    public void doBackTabZero(String nameFragment) {
          android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 
          if(fragmentManager.getBackStackEntryCount() > 1 ){
              fragmentManager.popBackStackImmediate(); 
          }else{
              super.onBackPressed();
          }
    }