Android-----DialogFragment的使用

一、DialogFragment是Android針對一些常用場景,對Fragment進行的一次封裝。

建立一個類繼承DialogFragment,創建一個方法,該方法返回Fragment自身,並且保存住外界傳入的類型,
public static MyDialogFragment getInstance(int type){
 MyDialogFragment f1=new MyDialogFragment ();
 Bundle bundle=new Bundle()'
 bundler.putInt("Dialog_Type",type);
 f1.setArgument(bundler);
 return f1;
}
//在onCreateDialog中寫入自己的業務邏輯,當前要實現的是根據外部傳入的類型,創建不同的Fragment
Dialog onCreateDialog(Bundle saveInstance){
 Dialog dialog=null;
 int dialogType=
 getArguments().getInt("Dialog_Type")//拿到通過getInstance方法保存在自身中的值
 switch(dialogType){
  case XX:
    return new AlertDialog()......
  break;
}
 return dialog;
}

在Activity中調用

MyFragment myFragment=MyFragment.getInstance();
if(myFragment!=null){
 myFragment.show(getFragmentManager(),tag);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章