DialogFragment在屏幕旋转时消失

时间:2014-07-18 10:04:17

标签: android android-dialogfragment

这个相当简单的对话框在屏幕旋转后自动消失,尽管我将setRetainInstance设置为true。任何想法都错了吗?

public class StreetDialog extends DialogFragment {

    public static StreetDialog newInstance(String[] values) {
        StreetDialog f = new StreetDialog();
        Bundle args = new Bundle();
        args.putStringArray("values", values);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final String[] values = getArguments().getStringArray("values");

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //build my dialog
        return builder.create();
    }

    @Override
    public void onDestroyView() {
        if (getDialog() != null && getRetainInstance())
            getDialog().setDismissMessage(null);

        super.onDestroyView();
    }
}

2 个答案:

答案 0 :(得分:2)

如果我没记错是正常行为。我通常会为show方法提供一个标记,当再次调用Activity的onCreate时,我会查找标记。如果片段!= null我在创建和显示新片段之前将其删除。在代码中,我通常做的是:

Fragment fragment = getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
if (fragment != null) {
  getSupportFragmentManager().beginTransaction().remove(fragment).commit();
}
new CustomDialogFragment().show(getSupportFragmentManager(), FRAGMENT_TAG );

答案 1 :(得分:0)

这是一个我认为解决和使用以下方法的最佳方法的问题:

创建一个静态方法来初始化一个Dialog,记住这是一个很好的做法,因为我们总是有默认的构造函数,而Bundle存储了Fragment的状态。 在onCreateDialog方法中,使用"构造函数方法"中传递的数据初始化AlertDialog。 在您的Activity中,您可以实现一个接口(因为我们无法保留它的引用,因为它可能在旋转设备时被销毁)。要打开对话框, 检查它是否已添加到FragmentManager否则展示。

在此处查看更多信息(葡萄牙语链接 - br):http://nglauber.blogspot.com.br/2013_10_01_archive.html

public class SimpleDialog extends DialogFragment implements OnClickListener {  

  private static final    String EXTRA_ID      = "id";  
  private static final   String EXTRA_MESSAGE = "message";  
  private static final   String EXTRA_TITLE   = "title";  
  private static final   String EXTRA_BUTTONS = "buttons";  
  private static final   String DIALOG_TAG    = "SimpleDialog";  

  private int dialogId;  

  public static SimpleDialog newDialog(int id,   
    String title, String message, int[] buttonTexts){  
    // Using the Bundle to save state  
    Bundle bundle  = new Bundle();  
    bundle.putInt(EXTRA_ID, id);  
    bundle.putString(EXTRA_TITLE, title);   
    bundle.putString(EXTRA_MESSAGE, message);  
    bundle.putIntArray(EXTRA_BUTTONS, buttonTexts);  

    SimpleDialog dialog = new SimpleDialog();  
    dialog.setArguments(bundle);  
    return dialog;   
  }  

  @Override  
  public Dialog onCreateDialog(Bundle savedInstanceState) {  

    String title = getArguments() .getString(EXTRA_TITLE);  
    String message = getArguments().getString(EXTRA_MESSAGE);  
    int[] buttons = getArguments().getIntArray(EXTRA_BUTTONS);  

    AlertDialog.Builder alertDialogBuilder =  new AlertDialog.Builder(getActivity());  
    alertDialogBuilder.setTitle(title);  
    alertDialogBuilder.setMessage(message);  

    switch (buttons.length) {  
      case 3:  
        alertDialogBuilder.setNeutralButton(buttons[2], this);  

      case 2:  
        alertDialogBuilder.setNegativeButton(buttons[1], this);  

      case 1:  
        alertDialogBuilder.setPositiveButton(buttons[0], this);  
    }      
    return alertDialogBuilder.create();  
  }  

  @Override  
  public void onClick(DialogInterface dialog, int which) {  
    // Your Activity must to implements this interface  
    ((FragmentDialogInterface)getActivity()).onClick(dialogId, which);  
  }  

  public void openDialog( FragmentManager supportFragmentManager) {  

    if (supportFragmentManager.findFragmentByTag( DIALOG_TAG) == null){  
        show(supportFragmentManager, DIALOG_TAG);  
    }    
  }  
  // Interface that was invoked by clicking the button  
  public interface FragmentDialogInterface {  
    void onClick(int id, int which);  
  }

打开活动中的对话框

public void openSimpleDialog(View v) {  
  SimpleDialog dialog = SimpleDialog.newDialog(  
    0,              // Id from dialog  
    "Alert",       // title
    "Message",     // menssage  
    new int[]{      // texts from buttons
      android.R.string.ok,   
      android.R.string.cancel });  
    dialog.openDialog(getSupportFragmentManager());  
  }  

  @Override  
  public void onClick(int id, int which) {  
    Toast.makeText(MainActivity.this,   
      "Button clicked"+ which, Toast.LENGTH_SHORT)  
        .show();  
  }