调试另一个问题时,我发现其中一个活动的onCreateView
方法被调用了两次。我是编程新手,不完全理解android
在加载活动时如何调用这些方法,但我觉得它不应该被调用两次。尽管减少了大部分代码,我仍然看到我的System.out
消息两次。
public class AddCourse extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_course);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new AddCourseFragment()).commit();
}
}
public static class AddCourseFragment extends Fragment {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_add_course,
container, false);
System.out.println("I see this TWICE!!!!");
return rootView;
}
}
}
这几乎与我的主要活动实现完全相同,但那一个不会通过 onCreateView
两次。有什么想法吗?
我被要求提供我的 activity_add_course xml...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.NsouthProductions.gradetrackerpro.AddCourse$AddCourseFragment"
android:id="@+id/AddCourseFrag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>