可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using android compatibility library (v4 revision 8). In the custom DialogFragment the overrided method onViewCreated is not getting called.For eg.
public class MyDialogFragment extends DialogFragment{ private String mMessage; public MyDialogFragment(String message) { mMessage = message; } @Override public Dialog onCreateDialog( Bundle savedInstanceState){ super.onCreateDialog(savedInstanceState); Log.d("TAG", "onCreateDialog"); setRetainInstance(true); //....do something } @Override public void onViewCreated(View view, Bundle savedInstanceState) { Log.d("TAG", "onViewCreated"); //...do something } }
onViewCreated is not getting logged.
回答1:
Well, the docs for onViewCreated state "Called immediately after onCreateView(LayoutInflater, ViewGroup, Bundle) has returned".
DialogFragment uses onCreateDialog and not onCreateView, so onViewCreated is not fired. (Would be my working theory, I haven't dived into the android source to confirm).
回答2:
From my testing, onViewCreated
isn't called if onCreateView returns null, which is the default behavior, so if you're not using onCreateView but manually calling setContentView
in onCreateDialog
, you can manually call onViewCreated
from onCreateDialog
:
@Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Dialog d = super.onCreateDialog(savedInstanceState); d.setContentView(R.layout.my_dialog); // ... do stuff.... onViewCreated(d.findViewById(R.id.dialog_content), savedInstanceState); return d; }
In this case, make sure the root element in my_dialog.xml
has android:id="@+id/dialog_content"
回答3:
You can see what's happening from the source code:
First, since you don't override onCreateView()
your fragment's view will be null. This can be seen from the source code of Fragment
-- the default returns null
:
// android.support.v4.app.Fragment.java @Nullable public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return null; }
Second, since you're view is null the FragmentManager
will not call onViewCreated()
. From the source code of FragmentManager
:
// android.support.v4.app.FragmentManager.java if (f.mView != null) { f.mInnerView = f.mView; // ... // only called if the fragments view is not null! f.onViewCreated(f.mView, f.mSavedFragmentState); } else { f.mInnerView = null; }
回答4:
According to the doc (Selecting Between Dialog or Embedding) and having it tested by myself, you can override OnCreateView, inflate it with your custom layout and return it. OnViewCreated will be launched
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.custom_layout, null); //do whatever return view; }