DialogFragment
onCreate()
中必须调用super.onCreate(savedInstanceState);
使用
newInstance()
方法来获取实例,使用setArguments来传递参数,避免在横竖屏切换的时候Fragment自动调用自己的无参构造函数,导致数据丢失。static MyDialogFragment newInstance(int num) {
MyDialogFragment f = new MyDialogFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
}Dialog重复
show()
或者重复dismiss()
会怎么样
mCustomDialog = CustomDialog.newInstance(); |
重复dismiss()
没反应,看源码有做判断
void dismissInternal(boolean allowStateLoss) { |
- 线程中创建和隐藏Dialog都没有问题,就是说Dialog的创建和隐藏不算是更新UI操作
- 不能再线程中创建Toast
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Toast含有一个TN的成员,TN成员初始化时会new一个Handler,但是handler创建时会判断该线程是否Looper.prepare()
过。
public Handler(Callback callback, boolean async) { |
- 线程中启动Activity,关闭Activity不算更新UI.