Gau*_*sth 29 android android-dialogfragment android-support-library
我正在使用android兼容库(v4修订版8).在自定义DialogFragment中,没有调用onViewCreated的覆盖方法.例如.
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
}
}
Run Code Online (Sandbox Code Playgroud)
onViewCreated未被记录.
Bar*_*rak 17
好吧,onViewCreated状态的文档"在onCreateView(LayoutInflater,ViewGroup,Bundle)之后立即调用"已经返回".
DialogFragment使用onCreateDialog而不是onCreateView,因此不会触发onViewCreated.(将是我的工作理论,我没有潜入android源来确认).
Car*_*mer 12
这就是我确保在 kotlin 中调用 onViewCreated 的方式:
class MyDialog: DialogFragment() {
private lateinit var dialogView: View
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
dialogView = LayoutInflater.from(context).inflate(R.layout.dialog, null)
val dialog = MaterialAlertDialogBuilder(context!!)
.setView(dialogView)
.create()
return dialog
}
// Need to return the view here or onViewCreated won't be called by DialogFragment, sigh
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return dialogView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// Yay it's now called!
}
override fun onDestroyView() {
dialogView = null
super.onDestroyView()
}
}
Run Code Online (Sandbox Code Playgroud)
从我的测试,onViewCreated不叫,如果onCreateView返回null,这是默认行为,所以如果你不使用onCreateView但手动调用setContentView中onCreateDialog,你可以手动调用onViewCreated来自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;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,请确保in元素中my_dialog.xml有android:id="@+id/dialog_content"
您可以从源代码中看到发生了什么:
首先,由于您不覆盖onCreateView()您的片段,因此视图将为null.这可以从源代码中Fragment看出- 默认返回null:
// android.support.v4.app.Fragment.java
@Nullable
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return null;
}
Run Code Online (Sandbox Code Playgroud)
其次,因为你的视图是null,所以FragmentManager不会调用onViewCreated().从源代码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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17019 次 |
| 最近记录: |