2013-11-27 48 views
1

我目前使用AlertDialog在我的应用程序中创建一个简单的对话框。我使用看起来像这样的代码:在屏幕旋转时重新创建AlertDialog的最佳方式是什么?

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setCancelable(false); 
builder.setTitle(DialogTitle); 
builder.setItems(R.array.options, new DialogInterface.OnClickListener() { 
@Override 
public void onClick(DialogInterface dialog, int which) { 
    switch (which) { 
     case 0: 
      reset(); 
     break; 
     case 1: 
     exit(); 
     break; 
     default: 
     break; 
     } 
    } 
}); 
builder.show(); 

我已阅读,最好的选择可能是创建一个扩展DialogFragment一个类,然后用我的DialogFragment,而不是我目前的执行情况。

任何人都可以证实这是最好的解决方案,建议更好的东西,可能给我一个例子吗?

谢谢。

+0

是的,使用'DialogFragment'将会是这些日子的适当方式。在d.android.com上的[Dialogs topic](http://developer.android.com/guide/topics/ui/dialogs.html#DialogFragment)中有文档和示例。拥有'DialogFragment'的好处是框架能够在配置改变时正确地管理它的状态,并且它变成一个可重用的组件。使用'android:configChanges'(如下所示)可能会产生一些不希望的副作用,并使代码库变得不太灵活。 –

+0

我用DialogFragment去了。在Dialogs主题中探讨后,我轻松实现了满足需求的一个。在我的测试活动中,看起来我现在可以使用“开箱即用”片段旋转屏幕。我还没有在我的应用程序中实现。谢谢MH。 –

回答

1
public class ListDialogFragment extends DialogFragment 
    { 
    // Use this instance of the interface to deliver action events 
    private listDialogListener mListener; 
    private String title; 
    private int items; 

    /** 
    * Create a new instance of EndGameDialogFragment, String dialogTitle 
    * and ing dialogListItems as an argument. 
    */ 
    static ListDialogFragment newInstance(String dialogTitle, int dialogListItems) 
    { 
     ListDialogFragment frag = new ListDialogFragment(); 

     // Supply num input as an argument. 
     Bundle args = new Bundle(); 
     args.putString("title", dialogTitle); 
     args.putInt("items", dialogListItems); 
     frag.setArguments(args); 

     return frag; 
    } 


    /* The activity that creates an instance of this dialog fragment must 
    * implement this interface in order to receive event callbacks. 
    * Each method passes the DialogFragment in case the host needs to query it. */ 
    public interface listDialogListener 
    { 
     public void onDialogClick(DialogFragment dialog, int which); 
    } 

    // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener 
    @Override 
    public void onAttach(Activity activity) 
    { 
     super.onAttach(activity); 
     // Verify that the host activity implements the callback interface 
     try 
     { 
      // Instantiate the NoticeDialogListener so we can send events to the host 
      mListener = (listDialogListener) activity; 
     } 
     catch (ClassCastException e) 
     { 
      // The activity doesn't implement the interface, throw exception 
      throw new ClassCastException(activity.toString() 
        + " must implement NoticeDialogListener"); 
     } 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    { 
     title = getArguments().getString("title"); //retrive the titleString 
     items = getArguments().getInt("items"); //retrive array of items for the list (from strings.xml) 

     //build the dialog 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle(title); 
     builder.setItems(items, new DialogInterface.OnClickListener() 
     { 
      public void onClick(DialogInterface dialog, int which) 
      { 
       switch(which) 
       { 
        case 0: 
         mListener.onDialogClick(ListDialogFragment.this, which); 
         break; 
        case 1: 
         mListener.onDialogClick(ListDialogFragment.this, which); 
         break; 
        default: 
         break; 
       } 
      } 
     }); 
     return builder.create(); 
    } 

} 
+0

我看不到任何代码正确地重新创建片段 –

+0

Hello Reneez,我不需要重新创建片段。我以前直接使用对话框,但我不知道用这种方法使用DialogFragment会给我所需的所有功能。当我按照上面的方式实现时,屏幕的旋转会自动处理片段生命周期,并且屏幕旋转时对话框不会消失,正如我以前的问题。 –

+0

片段生命周期 - http://developer.android.com/guide/components/fragments.html 在屏幕打开时,就像活动一样销毁并重新创建对话框。 –

0

您可以覆盖您的活动的onConfigurationChanged(Configuration newConfig)方法,以识别方向更改并在其打开时恢复AlertDialog。为此,您将在清单android:configChanges="orientation的活动定义中需要以下标记。一个很好的介绍可以找到here

+0

对于任何正在寻找实现的人。这是我最终使用的那个。 –

+0

然后你应该接受答案。 ;) – Baschi

+0

非常感谢Baschi。 –

相关问题