Why is my onCreateView method being called twice?
Asked Answered
T

1

15

While debugging another issue, I realized that the onCreateView method of one of my activities was being called twice. I'm new to programming and I don't fully understand how android calls these methods when the activity loads, but it doesn't seem right to me that it would be called twice. Eliminating most of my code, I still see my System.out message twice.

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;
        }       
    }
}

This is almost exactly like my main activity implementation, but that one doesn't go through onCreateView twice. Thoughts?

My activity_add_course xml was requested...

<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>
Touristy answered 8/4, 2014 at 2:17 Comment(4)
can you share your activity_add_course layout file?Pythagorean
@adavis , I've added the layout file. Is this happening because the layout file basically calls my fragment class? How should I stop it from being called twice?Touristy
looks like you're adding the fragment twice. If you declare it in the xml then you don't need to add it programmatically as well.Pythagorean
@adavis , hmm so which part of my code do I remove it from? How can I handle a null savedInstanceState without calling my fragment twice? Sorry, I'm still trying to grasp some of these basics.Touristy
P
30

Looks like you're adding the fragment twice. If you declare it in the xml then you don't need to add it programmatically as well.

You can remove this from your Activity's onCreate():

if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new AddCourseFragment()).commit();
        }
Pythagorean answered 8/4, 2014 at 2:38 Comment(1)
Thank you. After writing a long question here, I finally inferred that the savedInstanceState condition code is to ensure that the fragment gets inflated. But since the main layout inflates it anyway, this code isn't needed. I hope there are no other side effects. Thanks!Touristy

© 2022 - 2024 — McMap. All rights reserved.