Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCreateView Fragment not called

I have a custom DialogFragment with a FrameLayout container in which I want to put a Fragment but its view always return null, what can I do?

In DialogCreateAccount.java

   public class DialogCreateAccount extends DialogGeneral implements OnClickListener{

   @Override
   public Dialog onCreateDialog(Bundle savedInstanceState) {
    // TODO Auto-generated method stub

    Dialog dialog = super.onCreateDialog(savedInstanceState);

    //........


    return dialog;

   }


@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

     Fragment fragment = CreateAccountFragment.newInstance();


     FragmentTransaction ft = getFragmentManager().beginTransaction();

         if(fragment.getView()!=null){ // return always null

            ft.add(
                    getFrameContainer().getId(),
                    fragment
                    )
            .commit();
         }else{

         }

        Log.i("DialogCreateAccount", "fragment:" +fragment.getView());// return null
  }

In CreateAccountFragment.java

 public class CreateAccountFragment extends Fragment implements OnClickListener{

        public CreateAccountFragment() {
          // TODO Auto-generated constructor stub

        }

       public static CreateAccountFragment newInstance() {

             CreateAccountFragment f = new CreateAccountFragment();

             return f;
       }

       @Override
       public void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
         super.onCreate(savedInstanceState);
         Log.i("CreateAccountFragment", "onCreate");
         //onCreate is called


       }

       @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
          // TODO Auto-generated method stub

      Log.i("CreateAccountFragment", "onCreateView");
        //onCreateView not called

          mRootView = inflater.inflate(R.layout.dialog_create_an_account, container, false);




          return mRootView;


       }
      }

In Fragment onCreate is called and onCreateView is not called.

like image 324
Adriana Carelli Avatar asked Aug 22 '14 10:08

Adriana Carelli


People also ask

What does the onCreateView method return if a fragment doesn't have any UI?

These files contain only the onCreateView() method to inflate the UI of the fragment and returns the root of the fragment layout. If the fragment does not have any UI, it will return null.

Is onCreate called before onCreateView?

The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView() .

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

onCreate(Bundle) called to do initial creation of the fragment. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.

What is the difference between onCreateView and onViewCreated in fragment?

However, onCreateView() is called first and should only be used to inflate the fragment's view. onViewCreated() is called second and all logic pertaining to operations on the inflated view should be in this method.


1 Answers

Fragment getView() only returns a non-null view once onCreateView() has been run in the fragment lifecycle.

Merely instantiating a fragment object does not call any of its lifecycle callbacks. They will be called later when the fragment transaction executes.

Just put the fragment in the container without conditionally checking whether getView() returns non-null.

like image 181
laalto Avatar answered Sep 19 '22 10:09

laalto