在popBackStack之后片段仍然可见

时间:2016-09-06 20:49:48

标签: android android-fragments

在我的MainActivity中,FrameLayout(id = fragment_container)被onCreateFragmentA的活动取代。 FragmentA只包含一个按钮,并在MainActivity中调用onClick,以便将FrameLayout替换为FragmentBFragmentB包含RecyclerView并正确显示。我致电fragmentManager.popBackStack()以取回FragmentA。现在FragmentA显示在FragmentB之上(我仍然可以与之交互)。

调用FragmentB后,为什么popBackStack没有删除?

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private FragmentManager fragmentManager;

    @Override
    public void onBackPressed() {
        fragmentManager.popBackStack();
    }

    @Override
    public void onClick(View view) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, new FragmentB());
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //...

        fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        FragmentA fragment = FragmentA.getInstance(this); //register OnClickListener
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();
    }
//...
}

FragmentA

public class FragmentA extends Fragment {

    private View.OnClickListener onClickListener;

    public FragmentA() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_a, container, false);

        FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
        fab.setOnClickListener(onClickListener);

        return view;
    }

    public static FragmentA getInstance(View.OnClickListener onClickListener) {
        FragmentA fragmentA = new FragmentA();
        fragmentA.setOnClickListener(onClickListener);
        return fragmentA;
    }

    private void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }
}

FragmentB

public class FragmentB extends Fragment {

    //...

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.FragmentB, container, true);
        //manipulating Views...
        //...

        return view;
    }
}

2 个答案:

答案 0 :(得分:0)

问题出在FragmentB课程中。你不应该传递参数attachToRoot=true

请改为尝试:

public class FragmentB extends Fragment {

    //...

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //don't pass true
        View view = inflater.inflate(R.layout.FragmentB, container, false);
        //manipulating Views...
        //...

        return view;
    }
}

答案 1 :(得分:0)

改为使用popBackStackImmediate

@Override
public void onBackPressed() {
    fragmentManager.popBackStackImmediate();
}

来自popBackStackImmediate docs

  

与popBackStack()类似,但在调用内部立即执行操作。这就像之后调用executePendingTransactions()一样。