先看这样一个报错:
Caused by: android.view.InflateException: Binary XML file line #13 in com.example.kotlindemo:layout/layout_dialog_simple: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #13: Duplicate id 0x7f080152, tag null, or parent id 0xffffffff with another fragment for com.example.kotlindemo.ui.fragment.SimpleFragment
错位很明显:不能重复加载同一个id或者tag的fragment。这就意味着:重复new 含有同一个Fragment的dialog就会报错。
接下来看这样一个实例:
fun showSimpleDialog(view: View) {
if(!this::simpleDialog.isInitialized){
simpleDialog = SimpleDialog(this).apply {
setContentView(LayoutInflater.from(activity).inflate(R.layout.layout_dialog_simple, null))
}
}
simpleDialog.show()
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<fragment
android:id="@+id/simpleFragment"
android:name="com.example.kotlindemo.ui.fragment.SimpleFragment"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
fragment放进dialog中show:第一次
SimpleFragment: onAttach:
SimpleFragment: onCreate:
SimpleFragment: onStart:
SimpleFragment: onResume:
SimpleDialog: onCreate:
dimiss时:无日志
第二次show:无日志
因为此时一直处于onResume的状态。
所以,fragment放进dialog中则无法正常使用生命周期回调方法了。但是在Activity其它生命周期发生变化的时候,此时的fragment的生命周期发生了对应的同步变化。
也就是说:fragment放进dialog,其生命周期是与Activity同步的。(即便是fragment直接放进Activity的布局页面方式,其show和hide也是不会触发生命周期变化的)
这一点从源码也可以验证:
/**
* Called when the Fragment is visible to the user. This is generally
* tied to {@link Activity#onStart() Activity.onStart} of the containing
* Activity's lifecycle.
*/
@CallSuper
public void onStart() {
mCalled = true;
}
/**
* Called when the fragment is visible to the user and actively running.
* This is generally
* tied to {@link Activity#onResume() Activity.onResume} of the containing
* Activity's lifecycle.
*/
@CallSuper
public void onResume() {
mCalled = true;
}
借助这种方式,可以实现dialog生命周期的感知。(当然dialog使用lifecycle也是可以做到的)