【安卓筆記】使用DialogFragment託管dialog

rowandjj發表於2015-06-01

普通的AlertDialog在橫豎屏切換時會被銷燬,如果dialog上面有資料,也將丟失。解決方案是使用DialogFragment
使用dialogFragment通常需要複寫兩個方法:

onCreateView(LayoutInflater, ViewGroup, Bundle)
onCreateDialog(Bundle)

如果你想自定義dialog樣式,只需要複寫onCreateView,注入一個自定義的view即可,然後通過呼叫DialogFragment#show()方法即可。

這裡我們不需要自定義,只需要託管AlertDialog即可,所以我們僅僅需要複寫onCreateDialog方法。在這個方法內部我們需要通過AlertDialog.Builder構建一個dialog並返回,dialog的引數可以通過setArguments注入。具體程式碼如下:

package com.taobao.dialogfragmentdemo;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
public class AlertDialogFragment extends DialogFragment
{
    private static final String PARAM_TITLE = "title";
    private static final String PARAM_CONTENT = "content";
    private static DialogCallback mCallback;
    public AlertDialogFragment()
    {
    }
    public static AlertDialogFragment newInstance(String title,String content,DialogCallback callback)
    {
        AlertDialogFragment instance = new AlertDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString(PARAM_TITLE,title);
        bundle.putString(PARAM_CONTENT,content);
        instance.setArguments(bundle);
        mCallback = callback;
        return instance;
    }
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        Bundle params = getArguments();
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(params.getString(PARAM_TITLE));//沒有做非空判斷,按需新增
        builder.setMessage(params.getString(PARAM_CONTENT));
        builder.setPositiveButton("確定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                if(mCallback != null)
                    mCallback.onPostiveClick();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                if(mCallback != null)
                    mCallback.onNegativeClick();
            }
        });
        return builder.show();
    }
    public interface DialogCallback
    {
        public void onPostiveClick();
        public void onNegativeClick();
    }
}

想使用也很簡單:

AlertDialogFragment dialog = AlertDialogFragment.newInstance("標題", "這是fragment託管的alertdialog", new AlertDialogFragment.DialogCallback()
        {
            @Override
            public void onPostiveClick()
            {
                Toast.makeText(MainActivity.this,"確定",Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNegativeClick()
            {
                Toast.makeText(MainActivity.this,"取消",Toast.LENGTH_SHORT).show();
            }
        });
        dialog.show(getSupportFragmentManager(),"dialog");

還可以通過DialogFragment#show的第二個引數tag來找到對應的dialogFragment:

 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
 Fragment prevDialog = getSupportFragmentManager().findFragmentByTag("dialog");
 if(prevDialog != null)
 {
      transaction.remove(prevDialog);
 }

相關文章