1. 程式人生 > >Android:兩個Fragment在同一個Framelayout上切換顯示

Android:兩個Fragment在同一個Framelayout上切換顯示

private String[] TAGS = new String[]{"tag1", "tag2"};
private void showFragment(Fragment from, Fragment to, int position) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        boolean isAdded = to.isAdded();
        if (!isAdded) {
            transaction.hide(from).add(R.id.layout_fragment, to, TAGS[position]).show(to).commitAllowingStateLoss();
        } else {
            transaction.hide(from).show(to).commitAllowingStateLoss();
        }
    }