Android中存在相同id导致的BUG

2,657 阅读1分钟

越来越多的页面使用一个Activity,多个Fragment的方式来实现,如ViewPager+Fragment或Activity不同区域添加Fragment。 这时,如果不同Fragment中含有相同的id,且都被添加到了Activity上,则会出现要显示的内容出错的问题。 其实就是同一个Activity,不能有相同id的问题,只是如果是在xml直接定义工具会编译错误,而使用Fragment则不会编译错误。

public class RootActivity extends Activity {

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

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.home, new HomeFragment());
        transaction.commitAllowingStateLoss();

        Button btn = (Button) findViewById(R.id.change);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                Fragment secFragment = new SecondFragment();
                transaction.replace(R.id.sec, secFragment);
                transaction.commitAllowingStateLoss();
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <Button
        android:id="@+id/change"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/home"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
        <FrameLayout
            android:id="@+id/sec"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </FrameLayout>
    </FrameLayout>
</LinearLayout>
public class HomeFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mRootView = inflater.inflate(R.layout.fragment_common, container, false);
        TextView nameTxt = (TextView) mRootView.findViewById(R.id.name);
        nameTxt.setText("HOME");
        return mRootView;
    }
}
public class SecondFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mRootView = inflater.inflate(R.layout.fragment_common, container, false);
        TextView nameTxt = (TextView) mRootView.findViewById(R.id.name);
        nameTxt.setText("SECOND");
        return mRootView;
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.content, new SugFragment());
        transaction.commitAllowingStateLoss();
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#00ff00">
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"/>
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</LinearLayout>

HomeFragment和SecFragment会共用布局文件fragment_common,所以最终的RootActivity上会存在2个id为content的FrameLayout,所以在SecFragment中对content操作时导致找到的是HomeFragment中的。

public class SugFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mRootView = inflater.inflate(R.layout.fragment_common, container, false);
        TextView nameTxt = (TextView) mRootView.findViewById(R.id.name);
        nameTxt.setText("SUG");
        return mRootView;
    }
}

由代码可见,是要在Sec页显示SUG页面,但最终效果没有显示出来。