2014-09-04 46 views
0

在我的應用程序中,我有一個45「editText」。現在,當用戶旋轉設備時,對話框關閉,所有數據都消失了。所以我在Dialogclass中創建了一個「public static boolean isShow = false」變量,它的確是對話框是「show」,並且在我的MainActivity上我保存到包中,它的工作原理我的對話框沒有關閉,因爲我創建了一個新的對話框, isShow「= false。但所有數據都清除了。我的問題是我需要一個接一個保存所有45個editText嗎?如果我需要,有一種方法可以取消只對這個對話框的旋轉?如何在對話框類上保存實例狀態?

public class DialogSetting extends Dialog { 

public static boolean isShow = false; 

    public DialogSetting(Context context) { 
     super(context); 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.dialog_setting); 
     this.context = context; 

     this.show(); 
     isShow = true; 


     this.setOnCancelListener(new DialogInterface.OnCancelListener() 
     { 
      @Override 
      public void onCancel(DialogInterface dialog) 
      { 
       isShow = false; 
      } 
     }); 

    } 


public void close(View v){ 
isShow = false; 
this.dismiss(); 
} 

MainActivity:

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    // TODO Auto-generated method stub 
    super.onSaveInstanceState(outState); 

    outState.putBoolean("dialogSetting", DialogSetting.isShow); 
} 


@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onRestoreInstanceState(savedInstanceState); 


    DialogSetting.isShow = savedInstanceState.getBoolean("dialogSetting"); 

    if(DialogSetting.isShow){ 
    new DialogSetting(this); 
    } 

回答

0

首先:你應該從DialogFragment繼承你的對話框。如果你這樣做,FragmentManager將在轉動設備後重新創建碎片。

因此,這裏是你的對話框類應該怎麼樣子:

public class MyDialog extends DialogFragment { 

    public static MyDialog newInstance(){ 
     MyDialog f = new MyDialog(); 
     return f; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.dialog_setting, container, false); 
     getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 

     // Everything else that needs to be done to setup the dialog (findViewById, Listener etc.) 

     return view; 
    } 
} 

這裏是你如何需要調用對話框(稍微複雜):

FragmentTransaction ft = getFragmentManager().beginTransaction(); 
Fragment prev = getFragmentManager().findFragmentByTag("dialog"); 
if (prev != null) { 
    ft.remove(prev); 
} 
ft.addToBackStack(null); 
MyDialog dialogFragment = MyDialog.newInstance(); 
dialogFragment.show(ft, "dialog"); 

爲了防止由於任何數據丟失旋轉時,您需要將數據保存在對話框中的一個包中(基本上和您在Activity中一樣)。 下面是在保存EditText的輸入文本的一個基本的例子:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if(savedInstanceState != null){ 
     text = savedInstanceState.getString("et_text"); 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putString("et_text", mEditText.getText().toString()); 
} 

您可以在docs(click)閱讀更多關於DialogFragments

+0

我不明白什麼不同,什麼更好,在我的問題,我想跳過editText保存實例的一部分因爲我有很多edittext – Matt 2014-09-04 13:18:48

+0

好吧,這是好事。 'FragmentManager'也保留了'EditText'的輸入。所以你不必手動保存輸入。我的不好;) – reVerse 2014-09-04 14:34:29

+0

好吧,ID不知道謝謝:) – Matt 2014-09-05 01:00:01