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.
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.
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() .
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With