为什么在android中添加新片段时旧片段中的按钮仍然有效?

Wun*_*Wun 0 android android-fragments

我正在Android中开发。一个Activity 中有两个片段。该片段-A具有一个按钮,并且活动将显示片段-A第一。

我使用以下代码添加fragment-B

SecondFragment secondFragment = new SecondFragment ();
mActivity.getSupportFragmentManager().beginTransaction()
                            .add(R.id.container, secondFragment )
                            .addToBackStack(secondFragment .getClass().getName()).commit();
Run Code Online (Sandbox Code Playgroud)

但是在视图已经更改为fragment-B之后,当我触摸按钮的位置时,fragment-A 中OnClickListenerof仍然有效。button

我错过了什么吗?如果我不使用replace或 ,如何解决问题setClickble to false?提前致谢。

Sam*_*ert 5

This happens due to the transparency of events in fragments.

You could add the below attributes to the root view group of the second fragment to avoid passing click event to backstacked fragments

android:clickable="true"
android:focusable="true"
Run Code Online (Sandbox Code Playgroud)