我注意到在较新的Android Studio版本中,当我创建一个新活动时,模板总是在里面放一个片段,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
这样做有什么好处?我总是要删除片段,因为它会使代码变得杂乱无章。我知道片段在你有多标签或主细节结构时很有用。但是为什么它现在默认嵌入到每个创建的活动中?