Dialog和DialogFragment
转载请注明:http://blog.csdn.net/liaoqianchuan00/article/details/24124885
从Honeycomb开始引入了Fragment来支持重用一部分UI。同时Activity的showDialog和dismissDialog方法不建议再被使用了,而我们应当使用DialogFragment。在低版本中我们通过加入support library也同样可以使用DialogFragment。
我们先看我们是怎么来使用一般的dialog的。在这里我们显示一个最简单的AlertDialog。在点击按钮的时候显示。
setContentView(R.layout.activity_main);
Buttonbtn = (Button) findViewById(R.id.button1);
btn.setText("thisis third activity");
btn.setOnClickListener(newOnClickListener() {
@Override
publicvoid onClick(View v) {
AlertDialogdialog = new AlertDialog.Builder(ThirdActivity.this).setTitle("Dialog").setMessage("thisis a dialog").show();
}
});
运行我们的程序,没发现有什么问题,但是如果这个时候configration变化,就会导致Activity重建,然后之前显示的对话框就不见了。比如这个时候我们旋转屏幕,发现对话框不在了。查看log可以发现:
04-1917:30:06.999: E/WindowManager(14495): Activitycom.example.androidtest.ThirdActivity has leaked windowcom.android.internal.policy.impl.PhoneWindow$DecorView{42ca3c18 V.E.....R....... 0,0-1026,414} that was originally added here
之前我们的解决方法通常如下:
@Override
protectedvoid onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(dialog!= null && dialog.isShowing()) {
outState.putBoolean("SHOW_DIALOG",true);
}
}
在oncreate里面去判断SHOW_DIALOG是否为true,如果是就显示对话框。
使用DialogFragment我们就不需要考虑这些了。
btn.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
MyDialogFragmentmyDialog = new MyDialogFragment();
myDialog.show(getSupportFragmentManager(),"mydialog");
}
});
/////////////////////////////////////////////////////////////////////
public static class MyDialogFragment extends DialogFragment {
@Override
publicView onCreateView(LayoutInflater inflater, ViewGroup container,
BundlesavedInstanceState) {
Log.d("==========","MyDialogFragment : onCreateView");
Viewv = inflater.inflate(R.layout.mydialog , container, false);
returnv;
}
@Override
publicvoid onAttach(Activity activity) {
//TODO Auto-generated method stub
super.onAttach(activity);
Log.d("==========","MyDialogFragment : onAttach");
}
@Override
publicvoid onCreate(Bundle savedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("==========","MyDialogFragment : onCreate");
}
@Override
publicvoid onDestroyView() {
//TODO Auto-generated method stub
super.onDestroyView();
Log.d("==========","MyDialogFragment : onDestroyView");
}
@Override
publicvoid onDetach() {
//TODO Auto-generated method stub
super.onDetach();
Log.d("==========","MyDialogFragment : onDetach");
}
@Override
publicvoid onStart() {
//TODO Auto-generated method stub
super.onStart();
Log.d("==========","MyDialogFragment : onStart");
}
@Override
publicvoid onStop() {
//TODO Auto-generated method stub
super.onStop();
Log.d("==========","MyDialogFragment : onStop");
}
}
通过Log,我们可以看见最开始DialogFragment创建,再到旋转屏幕后再次被创建的过程。
04-1917:45:41.289: D/==========(16156): MyDialogFragment : onAttach
04-1917:45:41.299: D/==========(16156): MyDialogFragment : onCreate
04-1917:45:41.299: D/==========(16156): MyDialogFragment : onCreateView
04-1917:45:41.309: D/==========(16156): MyDialogFragment : onStart
04-1917:45:50.619: D/==========(16156): MyDialogFragment : onStop
04-1917:45:50.619: D/==========(16156): third activity on destroy
04-1917:45:50.619:D/==========(16156): MyDialogFragment : onDestroyView
04-1917:45:50.619: D/==========(16156): MyDialogFragment : onDetach
04-1917:45:50.639: D/==========(16156): MyDialogFragment : onAttach
04-1917:45:50.639: D/==========(16156): MyDialogFragment : onCreate
04-1917:45:50.659: D/==========(16156): MyDialogFragment : onCreateView
04-1917:45:50.659: D/==========(16156): MyDialogFragment : onStart